← Home
For problem statement at 2000-2999/2000-2099/2060-2069/2061/problemF1.txt this is a correct solution, but verifier at 2000-2999/2000-2099/2060-2069/2061/verifierF1.go ends with mismatch at test 4: expected 2, got -1
exit status 1 can you fix the verifier? ```go
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var t int
	if _, err := fmt.Fscan(in, &t); err != nil {
		return
	}

	for i := 0; i < t; i++ {
		var s, target string
		fmt.Fscan(in, &s)
		fmt.Fscan(in, &target)

		if len(s) != len(target) {
			fmt.Fprintln(out, -1)
			continue
		}

		res := solve(s, target)
		fmt.Fprintln(out, res)
	}
}

type Block struct {
	Length int
	Kind   int
}

func getBlocks(s string) []Block {
	if len(s) == 0 {
		return nil
	}
	var blocks []Block
	curLen := 0
	curKind := int(s[0] - '0')
	for i := 0; i < len(s); i++ {
		k := int(s[i] - '0')
		if k == curKind {
			curLen++
		} else {
			blocks = append(blocks, Block{Length: curLen, Kind: curKind})
			curKind = k
			curLen = 1
		}
	}
	blocks = append(blocks, Block{Length: curLen, Kind: curKind})
	return blocks
}

func solve(s, target string) int64 {
	sBlocks := getBlocks(s)
	tBlocks := getBlocks(target)

	var sInd0, sInd1 []int
	for i, b := range sBlocks {
		if b.Kind == 0 {
			sInd0 = append(sInd0, i)
		} else {
			sInd1 = append(sInd1, i)
		}
	}

	var tInd0, tInd1 []int
	for i, b := range tBlocks {
		if b.Kind == 0 {
			tInd0 = append(tInd0, i)
		} else {
			tInd1 = append(tInd1, i)
		}
	}

	ranks := make([]int, len(sBlocks))
	if !assign(sBlocks, tBlocks, sInd0, tInd0, ranks) {
		return -1
	}
	if !assign(sBlocks, tBlocks, sInd1, tInd1, ranks) {
		return -1
	}

	return countInversions(ranks)
}

func assign(sB, tB []Block, sIdx, tIdx []int, ranks []int) bool {
	si, ti := 0, 0
	curSum := 0
	for ti < len(tIdx) {
		target := tB[tIdx[ti]].Length
		for si < len(sIdx) && curSum < target {
			idx := sIdx[si]
			curSum += sB[idx].Length
			ranks[idx] = tIdx[ti]
			si++
		}
		if curSum != target {
			return false
		}
		curSum = 0
		ti++
	}
	if si < len(sIdx) {
		return false
	}
	return true
}

func countInversions(arr []int) int64 {
	maxVal := 0
	for _, v := range arr {
		if v > maxVal {
			maxVal = v
		}
	}
	bit := make([]int, maxVal+2)
	var ans int64
	for i, v := range arr {
		x := v + 1
		countLe := query(bit, x)
		ans += int64(i - countLe)
		update(bit, x, 1)
	}
	return ans
}

func update(bit []int, idx, val int) {
	for idx < len(bit) {
		bit[idx] += val
		idx += idx & -idx
	}
}

func query(bit []int, idx int) int {
	s := 0
	for idx > 0 {
		s += bit[idx]
		idx -= idx & -idx
	}
	return s
}
```