← Home
For problem statement at 0-999/0-99/30-39/30/problemD.txt this is a correct solution, but verifier at 0-999/0-99/30-39/30/verifierD.go ends with case 8 failed: expected 11.0000000000 got 9.211102551
input:
3 3
-1 1 5 3 
-3
exit status 1 can you fix the verifier? package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
)

func main() {
	in := bufio.NewReader(os.Stdin)
	var n, k int
	if _, err := fmt.Fscan(in, &n, &k); err != nil {
		return
	}

	x := make([]float64, n+1)
	for i := 0; i <= n; i++ {
		var val float64
		fmt.Fscan(in, &val)
		x[i] = val
	}

	var y float64
	fmt.Fscan(in, &y)

	S_x := x[n]
	S_y := y

	A_unsorted := make([]float64, n)
	copy(A_unsorted, x[:n])

	var startVal float64
	if k <= n {
		startVal = A_unsorted[k-1]
	}

	A := make([]float64, n)
	copy(A, A_unsorted)
	sort.Float64s(A)

	dist := func(px float64) float64 {
		return math.Hypot(px-S_x, S_y)
	}

	if k == n+1 {
		ans := dist(A[0])
		if n > 1 {
			ans = A[n-1] - A[0] + math.Min(dist(A[0]), dist(A[n-1]))
		}
		fmt.Printf("%.9f\n", ans)
		return
	}

	startIdx := 0
	for i := 0; i < n; i++ {
		if A[i] == startVal {
			startIdx = i + 1
			break
		}
	}

	ans := math.Inf(1)
	A_start := startVal

	for i := 0; i < n; i++ {
		target := A[i]
		c1 := (A_start - A[0]) + (A[n-1] - A[0]) + (A[n-1] - target)
		c2 := (A[n-1] - A_start) + (A[n-1] - A[0]) + (target - A[0])
		cost := math.Min(c1, c2) + dist(target)
		if cost < ans {
			ans = cost
		}
	}

	for u := 1; u < n; u++ {
		if startIdx <= u {
			for _, xIdx := range []int{0, u - 1} {
				X := A[xIdx]
				for _, yIdx := range []int{u, n - 1} {
					Y := A[yIdx]
					var CostX float64
					if xIdx == u-1 {
						CostX = A_start - A[0] + A[u-1] - A[0]
					} else {
						CostX = A[u-1] - A_start + A[u-1] - A[0]
					}
					CostY := A[n-1] - A[u]
					cost := CostX + CostY + dist(X) + dist(Y)
					if cost < ans {
						ans = cost
					}
				}
			}
		} else {
			for _, xIdx := range []int{0, u - 1} {
				X := A[xIdx]
				for _, yIdx := range []int{u, n - 1} {
					Y := A[yIdx]
					CostX := A[u-1] - A[0]
					var CostY float64
					if yIdx == u {
						CostY = A[n-1] - A_start + A[n-1] - A[u]
					} else {
						CostY = A_start - A[u] + A[n-1] - A[u]
					}
					cost := CostX + CostY + dist(X) + dist(Y)
					if cost < ans {
						ans = cost
					}
				}
			}
		}
	}

	fmt.Printf("%.9f\n", ans)
}