← Home
For problem statement at 0-999/600-699/650-659/653/problemC.txt this is a correct solution, but verifier at 0-999/600-699/650-659/653/verifierC.go ends with All tests passed can you fix the verifier? package main

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

func check(a []int, n int, bads []int, i, j int) bool {
	var aff [4]int
	m := 0
	cands := [4]int{i - 1, i, j - 1, j}
	for z := 0; z < 4; z++ {
		p := cands[z]
		if p < 0 || p >= n-1 {
			continue
		}
		dup := false
		for k := 0; k < m; k++ {
			if aff[k] == p {
				dup = true
				break
			}
		}
		if !dup {
			aff[m] = p
			m++
		}
	}

	for _, b := range bads {
		found := false
		for k := 0; k < m; k++ {
			if aff[k] == b {
				found = true
				break
			}
		}
		if !found {
			return false
		}
	}

	ai, aj := a[i], a[j]
	for k := 0; k < m; k++ {
		p := aff[k]
		q := p + 1

		x := a[p]
		if p == i {
			x = aj
		} else if p == j {
			x = ai
		}

		y := a[q]
		if q == i {
			y = aj
		} else if q == j {
			y = ai
		}

		if (p & 1) == 0 {
			if x >= y {
				return false
			}
		} else {
			if x <= y {
				return false
			}
		}
	}

	return true
}

func main() {
	data, _ := io.ReadAll(os.Stdin)
	pos := 0
	readInt := func() int {
		for pos < len(data) && (data[pos] < '0' || data[pos] > '9') {
			pos++
		}
		x := 0
		for pos < len(data) && data[pos] >= '0' && data[pos] <= '9' {
			x = x*10 + int(data[pos]-'0')
			pos++
		}
		return x
	}

	n := readInt()
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = readInt()
	}

	bads := make([]int, 0, 5)
	for i := 0; i < n-1; i++ {
		if (i&1) == 0 {
			if a[i] >= a[i+1] {
				bads = append(bads, i)
			}
		} else {
			if a[i] <= a[i+1] {
				bads = append(bads, i)
			}
		}
	}

	out := bufio.NewWriterSize(os.Stdout, 1<<20)
	if len(bads) > 4 {
		fmt.Fprint(out, 0)
		out.Flush()
		return
	}

	inS := make([]bool, n)
	S := make([]int, 0, 8)
	for _, p := range bads {
		if !inS[p] {
			inS[p] = true
			S = append(S, p)
		}
		if !inS[p+1] {
			inS[p+1] = true
			S = append(S, p+1)
		}
	}

	var ans int64
	for _, s := range S {
		for t := 0; t < n; t++ {
			if t == s {
				continue
			}
			if check(a, n, bads, s, t) {
				if s < t || !inS[t] {
					ans++
				}
			}
		}
	}

	fmt.Fprint(out, ans)
	out.Flush()
}