For problem statement at 0-999/700-799/790-799/798/problemD.txt this is a correct solution, but verifier at 0-999/700-799/790-799/798/verifierD.go ends with test 2 failed
expected: 3
1 2 4
got: 3
3 1 2
exit status 1 can you fix the verifier? package main
import (
"bufio"
"container/heap"
"fmt"
"os"
"sort"
)
type FastScanner struct {
r *bufio.Reader
}
func NewFastScanner() *FastScanner {
return &FastScanner{r: bufio.NewReaderSize(os.Stdin, 1<<20)}
}
func (fs *FastScanner) NextInt() int64 {
sign := int64(1)
val := int64(0)
c, _ := fs.r.ReadByte()
for (c < '0' || c > '9') && c != '-' {
c, _ = fs.r.ReadByte()
}
if c == '-' {
sign = -1
c, _ = fs.r.ReadByte()
}
for c >= '0' && c <= '9' {
val = val*10 + int64(c-'0')
c, _ = fs.r.ReadByte()
}
return val * sign
}
type Item struct {
key float64
idx int
a int64
b int64
}
type MinHeap []Item
func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i].key < h[j].key }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MinHeap) Push(x interface{}) { *h = append(*h, x.(Item)) }
func (h *MinHeap) Pop() interface{} {
old := *h
n := len(old)
it := old[n-1]
*h = old[:n-1]
return it
}
func buildTopRIndices(a, b []int64, r int, alpha float64) ([]int, int64, int64) {
n := len(a)
tiny := 1e-9
h := &MinHeap{}
heap.Init(h)
var sumA, sumB int64
for i := 0; i < n; i++ {
key := alpha*float64(a[i]) + (1.0-alpha)*float64(b[i]) + float64(i)*tiny
if h.Len() < r {
heap.Push(h, Item{key: key, idx: i, a: a[i], b: b[i]})
sumA += a[i]
sumB += b[i]
} else {
if key > (*h)[0].key {
top := heap.Pop(h).(Item)
sumA -= top.a
sumB -= top.b
heap.Push(h, Item{key: key, idx: i, a: a[i], b: b[i]})
sumA += a[i]
sumB += b[i]
}
}
}
indices := make([]int, h.Len())
for i := 0; i < len(indices); i++ {
it := heap.Pop(h).(Item)
indices[i] = it.idx
}
return indices, sumA, sumB
}
func slopeSumTopR(a, b []int64, r int, alpha float64) int64 {
n := len(a)
tiny := 1e-9
h := &MinHeap{}
heap.Init(h)
var slopeSum int64
for i := 0; i < n; i++ {
key := alpha*float64(a[i]) + (1.0-alpha)*float64(b[i]) + float64(i)*tiny
if h.Len() < r {
heap.Push(h, Item{key: key, idx: i, a: a[i], b: b[i]})
slopeSum += (a[i] - b[i])
} else {
if key > (*h)[0].key {
top := heap.Pop(h).(Item)
slopeSum -= (top.a - top.b)
heap.Push(h, Item{key: key, idx: i, a: a[i], b: b[i]})
slopeSum += (a[i] - b[i])
}
}
}
return slopeSum
}
func checkAndPrint(S []int, a, b []int64, sumAAll, sumBAll int64, r int) bool {
if len(S) > r {
S = S[:r]
}
used := make([]bool, len(a))
var sa, sb int64
for _, idx := range S {
if !used[idx] {
used[idx] = true
sa += a[idx]
sb += b[idx]
}
}
if 2*sa > sumAAll && 2*sb > sumBAll {
fmt.Println(len(S))
for i := 0; i < len(S); i++ {
if i > 0 {
fmt.Print(" ")
}
fmt.Print(S[i] + 1)
}
fmt.Println()
return true
}
return false
}
func padToR(base []int, order []int, r int) []int {
if len(base) >= r {
return base[:r]
}
used := make(map[int]bool, len(base))
for _, x := range base {
used[x] = true
}
out := make([]int, 0, r)
out = append(out, base...)
for _, idx := range order {
if !used[idx] {
out = append(out, idx)
used[idx] = true
if len(out) == r {
break
}
}
}
return out
}
func main() {
in := NewFastScanner()
n := int(in.NextInt())
a := make([]int64, n)
b := make([]int64, n)
var sumAAll, sumBAll int64
for i := 0; i < n; i++ {
a[i] = in.NextInt()
sumAAll += a[i]
}
for i := 0; i < n; i++ {
b[i] = in.NextInt()
sumBAll += b[i]
}
r := n/2 + 1
// Precompute orders
idxAll := make([]int, n)
for i := 0; i < n; i++ {
idxAll[i] = i
}
// by d = a-b ascending
byD := make([]int, n)
copy(byD, idxAll)
sort.Slice(byD, func(i, j int) bool {
di := a[byD[i]] - b[byD[i]]
dj := a[byD[j]] - b[byD[j]]
if di == dj {
return byD[i] < byD[j]
}
return di < dj
})
// by a+b descending for padding
byApBDesc := make([]int, n)
copy(byApBDesc, idxAll)
sort.Slice(byApBDesc, func(i, j int) bool {
si := a[byApBDesc[i]] + b[byApBDesc[i]]
sj := a[byApBDesc[j]] + b[byApBDesc[j]]
if si == sj {
return byApBDesc[i] < byApBDesc[j]
}
return si > sj
})
// Alpha-based search
low, high := 0.0, 1.0
for it := 0; it < 28; it++ {
mid := (low + high) / 2.0
ss := slopeSumTopR(a, b, r, mid)
G := 2*ss - (sumAAll - sumBAll)
if G < 0 {
low = mid
} else {
high = mid
}
}
var alphaCandidates []float64
addAlpha := func(x float64) {
if x < 0 {
x = 0
}
if x > 1 {
x = 1
}
alphaCandidates = append(alphaCandidates, x)
}
addAlpha(0.0)
addAlpha(1.0)
addAlpha(low)
addAlpha(high)
addAlpha((low + high) / 2.0)
addAlpha(low - 1e-6)
addAlpha(high + 1e-6)
addAlpha(0.25)
addAlpha(0.5)
addAlpha(0.75)
seenAlpha := make(map[int64]bool)
for _, alpha := range alphaCandidates {
key := int64(alpha * 1e12)
if seenAlpha[key] {
continue
}
seenAlpha[key] = true
S, sa, sb := buildTopRIndices(a, b, r, alpha)
if 2*sa > sumAAll && 2*sb > sumBAll {
fmt.Println(len(S))
for i := 0; i < len(S); i++ {
if i > 0 {
fmt.Print(" ")
}
fmt.Print(S[i] + 1)
}
fmt.Println()
return
}
}
// Structured candidates based on byD
m := n / 2
// L and R halves
L := make([]int, 0, m)
R := make([]int, 0, m)
for i := 0; i < m; i++ {
L = append(L, byD[i])
}
for i := n - 1; i >= n-m; i-- {
if i >= 0 {
R = append(R, byD[i])
}
}
// L plus best A from R
if len(L) < r {
best := -1
var bestA int64 = -1
for _, idx := range R {
if a[idx] > bestA {
bestA = a[idx]
best = idx
}
}
cand := append([]int{}, L...)
if best != -1 {
cand = append(cand, best)
}
cand = padToR(cand, byApBDesc, r)
if checkAndPrint(cand, a, b, sumAAll, sumBAll, r) {
return
}
}
// R plus best B from L
if len(R) < r {
best := -1
var bestB int64 = -1
for _, idx := range L {
if b[idx] > bestB {
bestB = b[idx]
best = idx
}
}
cand := append([]int{}, R...)
if best != -1 {
cand = append(cand, best)
}
cand = padToR(cand, byApBDesc, r)
if checkAndPrint(cand, a, b, sumAAll, sumBAll, r) {
return
}
}
// Odds in byD
odds := []int{}
for i := 0; i < n; i += 2 {
odds = append(odds, byD[i])
}
if len(odds) < r {
odds = append(odds, byD[n-1])
}
odds = padToR(odds, byApBDesc, r)
if checkAndPrint(odds, a, b, sumAAll, sumBAll, r) {
return
}
// Evens in byD
evens := []int{}
for i := 1; i < n; i += 2 {
evens = append(evens, byD[i])
}
if len(evens) < r {
evens = append(evens, byD[0])
}
evens = padToR(evens, byApBDesc, r)
if checkAndPrint(evens, a, b, sumAAll, sumBAll, r) {
return
}
// Pick per pair by larger a
candA := []int{}
for i := 0; i < m; i++ {
l := byD[i]
rg := byD[n-1-i]
if a[l] >= a[rg] {
candA = append(candA, l)
} else {
candA = append(candA, rg)
}
}
if n%2 == 1 {
candA = append(candA, byD[m])
}
candA = padToR(candA, byApBDesc, r)
if checkAndPrint(candA, a, b, sumAAll, sumBAll, r) {
return
}
// Pick per pair by larger b
candB := []int{}
for i := 0; i < m; i++ {
l := byD[i]
rg := byD[n-1-i]
if b[l] >= b[rg] {
candB = append(candB, l)
} else {
candB = append(candB, rg)
}
}
if n%2 == 1 {
candB = append(candB, byD[m])
}
candB = padToR(candB, byApBDesc, r)
if checkAndPrint(candB, a, b, sumAAll, sumBAll, r) {
return
}
// Fallback: top r by a+b
cand := make([]int, 0, r)
for i := 0; i < r && i < n; i++ {
cand = append(cand, byApBDesc[i])
}
if checkAndPrint(cand, a, b, sumAAll, sumBAll, r) {
return
}
// As ultimate fallback (shouldn't happen), output first r indices
cand = make([]int, 0, r)
for i := 0; i < r && i < n; i++ {
cand = append(cand, i)
}
fmt.Println(len(cand))
for i := 0; i < len(cand); i++ {
if i > 0 {
fmt.Print(" ")
}
fmt.Print(cand[i] + 1)
}
fmt.Println()
}