For problem statement at 2000-2999/2000-2099/2040-2049/2045/problemD.txt this is a correct solution, but verifier at 2000-2999/2000-2099/2040-2049/2045/verifierD.go ends with case 1 mismatch: expected 29 got 28
input:
5 4 2 9 1
1 2 4 2 1
exit status 1 can you fix the verifier? package main
import (
"bufio"
"fmt"
"os"
"sort"
)
type State struct {
cost int64
y int
s int64
}
var bit []int64
var modified []int
func initBIT(n int) {
bit = make([]int64, n+2)
for i := range bit {
bit[i] = -1
}
modified = make([]int, 0, 1000)
}
func query(y int) int64 {
res := int64(-1)
for i := y; i > 0; i -= i & -i {
if bit[i] > res {
res = bit[i]
}
}
return res
}
func update(y int, s int64) {
for i := y; i < len(bit); i += i & -i {
if s > bit[i] {
bit[i] = s
}
}
modified = append(modified, y)
}
func clearBIT() {
for _, y := range modified {
for i := y; i < len(bit); i += i & -i {
bit[i] = -1
}
}
modified = modified[:0]
}
func main() {
reader := bufio.NewReader(os.Stdin)
var N int
var D, Ts, Tf, Tw int64
fmt.Fscan(reader, &N, &D, &Ts, &Tf, &Tw)
P := make([]int64, N+1)
pref := make([]int64, N+1)
for i := 1; i <= N; i++ {
fmt.Fscan(reader, &P[i])
pref[i] = pref[i-1] + P[i]
}
Tw2 := 2 * Tw
advance := func(y int, s int64, cost int64, req int64) (int, int64, int64, bool) {
if s >= req {
return y, s, cost, true
}
diff := req - s
target := diff + pref[y]
left, right := y+1, N
ans := -1
for left <= right {
mid := left + (right-left)/2
if pref[mid] >= target {
ans = mid
right = mid - 1
} else {
left = mid + 1
}
}
if ans == -1 {
return N, s + pref[N] - pref[y], cost + int64(N-y)*Tw2, false
}
return ans, s + pref[ans] - pref[y], cost + int64(ans-y)*Tw2, true
}
initBIT(N)
frontier := []State{{cost: 0, y: 1, s: P[1]}}
for i := 1; i < N; i++ {
var nextFrontier []State
for _, state := range frontier {
y_swim, s_swim, cost_swim, ok1 := advance(state.y, state.s, state.cost, D)
if ok1 {
s_swim -= D
cost_swim += Ts
if y_swim < i+1 {
s_swim += pref[i+1] - pref[y_swim]
y_swim = i+1
}
nextFrontier = append(nextFrontier, State{cost_swim, y_swim, s_swim})
}
y_fly, s_fly, cost_fly, ok2 := advance(state.y, state.s, state.cost, 1)
if ok2 {
s_fly = 0
cost_fly += Tf
if y_fly < i+1 {
s_fly += pref[i+1] - pref[y_fly]
y_fly = i+1
}
nextFrontier = append(nextFrontier, State{cost_fly, y_fly, s_fly})
}
}
sort.Slice(nextFrontier, func(a, b int) bool {
if nextFrontier[a].cost != nextFrontier[b].cost {
return nextFrontier[a].cost < nextFrontier[b].cost
}
if nextFrontier[a].y != nextFrontier[b].y {
return nextFrontier[a].y < nextFrontier[b].y
}
return nextFrontier[a].s > nextFrontier[b].s
})
frontier = frontier[:0]
for _, st := range nextFrontier {
if query(st.y) >= st.s {
continue
}
frontier = append(frontier, st)
update(st.y, st.s)
}
clearBIT()
if len(frontier) > 500 {
frontier = frontier[:500]
}
}
ans := int64(-1)
for _, st := range frontier {
if ans == -1 || st.cost < ans {
ans = st.cost
}
}
fmt.Println(ans)
}