← Home
For problem statement at 1000-1999/1000-1099/1090-1099/1092/problemD2.txt this is a correct solution, but verifier at 1000-1999/1000-1099/1090-1099/1092/verifierD2.go ends with All 100 tests passed can you fix the verifier? package main

import (
	"io"
	"os"
	"sort"
)

type Pair struct {
	h   int
	idx int
}

func main() {
	data, _ := io.ReadAll(os.Stdin)
	p := 0
	nextInt := func() int {
		for p < len(data) && (data[p] < '0' || data[p] > '9') && data[p] != '-' {
			p++
		}
		sign := 1
		if data[p] == '-' {
			sign = -1
			p++
		}
		v := 0
		for p < len(data) && data[p] >= '0' && data[p] <= '9' {
			v = v*10 + int(data[p]-'0')
			p++
		}
		return v * sign
	}

	n := nextInt()
	a := make([]int, n)
	pairs := make([]Pair, n)
	maxH := 0
	for i := 0; i < n; i++ {
		a[i] = nextInt()
		pairs[i] = Pair{a[i], i}
		if a[i] > maxH {
			maxH = a[i]
		}
	}

	sort.Slice(pairs, func(i, j int) bool {
		return pairs[i].h < pairs[j].h
	})

	parent := make([]int, n)
	size := make([]int, n)
	active := make([]bool, n)
	for i := 0; i < n; i++ {
		parent[i] = i
		size[i] = 1
	}

	var find func(int) int
	find = func(x int) int {
		if parent[x] != x {
			parent[x] = find(parent[x])
		}
		return parent[x]
	}

	oddCount := 0

	union := func(x, y int) {
		rx := find(x)
		ry := find(y)
		if rx == ry {
			return
		}
		oddCount -= size[rx] & 1
		oddCount -= size[ry] & 1
		if size[rx] < size[ry] {
			rx, ry = ry, rx
		}
		parent[ry] = rx
		size[rx] += size[ry]
		oddCount += size[rx] & 1
	}

	activate := func(i int) {
		active[i] = true
		parent[i] = i
		size[i] = 1
		oddCount++
		if i > 0 && active[i-1] {
			union(i, i-1)
		}
		if i+1 < n && active[i+1] {
			union(i, i+1)
		}
	}

	i := 0
	for i < n {
		j := i
		h := pairs[i].h
		for j < n && pairs[j].h == h {
			activate(pairs[j].idx)
			j++
		}
		if h != maxH && oddCount > 0 {
			os.Stdout.Write([]byte("NO"))
			return
		}
		i = j
	}

	os.Stdout.Write([]byte("YES"))
}