-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10271.go
60 lines (53 loc) · 1 KB
/
10271.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// UVa 10271 - Chopsticks
package main
import (
"fmt"
"math"
"os"
)
var (
bad []int
dp [][]int
)
func doDP(k, n int) int {
if dp[k][n] == -1 {
switch {
case n < 3*k:
dp[k][n] = math.MaxInt32
case k == 0:
dp[k][n] = 0
default:
dp[k][n] = min(doDP(k, n-1), doDP(k-1, n-2)+bad[n-1])
}
}
return dp[k][n]
}
func solve(k, n int, chopsticks []int) int {
bad = make([]int, n)
for i := 1; i < n; i++ {
bad[n-i] = (chopsticks[i] - chopsticks[i-1]) * (chopsticks[i] - chopsticks[i-1])
}
dp = make([][]int, k+1)
for i := range dp {
dp[i] = make([]int, n+1)
for j := range dp[i] {
dp[i][j] = -1
}
}
return doDP(k, n)
}
func main() {
in, _ := os.Open("10271.in")
defer in.Close()
out, _ := os.Create("10271.out")
defer out.Close()
var t, k, n int
for fmt.Fscanf(in, "%d", &t); t > 0; t-- {
fmt.Fscanf(in, "%d%d", &k, &n)
chopsticks := make([]int, n)
for i := range chopsticks {
fmt.Fscanf(in, "%d", &chopsticks[i])
}
fmt.Fprintln(out, solve(k+8, n, chopsticks))
}
}