-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQuantify.java
More file actions
108 lines (86 loc) · 3.07 KB
/
Quantify.java
File metadata and controls
108 lines (86 loc) · 3.07 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package code;
import java.io.BufferedInputStream;
import java.util.*;
/**
* Created by Inno Fang on 2017/6/3.
*/
public class Quantify {
private static final int MAXN = 100 + 5;
private static final int INF = 0x6ffffff;
private static int[] arr = new int[MAXN]; // 数列
private static int[] prefixSum = new int[MAXN]; // 前缀和
private static int[][] sumOfSquares = new int[MAXN][MAXN]; // 保存所有分块中数的差的平方的和
private static int[][] dp = new int[MAXN][10 + 5]; // 保存每个自分块的最优情况
private static Scanner in = new Scanner(new BufferedInputStream(System.in));
public static void main(String[] args) {
while (in.hasNextInt()) {
int cas = in.nextInt();
for (int i = 0; i < cas; i++) {
int count = in.nextInt();
int s = in.nextInt();
/* 初始化数列 */
initArray(count);
/* 将数列排序 */
Arrays.sort(arr, 1, count + 1);
/* 计算并保存前缀和 */
initPrefixSum(count);
/* 计算并保存所有分块中的数的差的平方的和 */
initSumOfSquares(count);
/* 使用动态规划 */
dynamicProgramming(count, s);
}
}
in.close();
}
private static void initArray(int count) {
/* input number */
arr[0] = 0;
for (int i = 1; i <= count; i++) {
arr[i] = in.nextInt();
}
}
private static void initPrefixSum(int count) {
for (int i = 0; i <= count; i++) {
prefixSum[i] = 0;
}
for (int i = 1; i <= count; i++) {
prefixSum[i] = prefixSum[i - 1] + arr[i];
}
}
private static void initSumOfSquares(int count) {
for (int i = 1; i <= count; i++) {
for (int j = 1; j <= count; j++) {
sumOfSquares[i][j] = 0;
}
}
for (int i = 1; i < count; i++) {
for (int j = i + 1; j <= count; j++) {
int aver = averageOf(i, j);
for (int k = i; k <= j; k++) {
sumOfSquares[i][j] += (arr[k] - aver) * (arr[k] - aver);
}
}
}
}
private static int averageOf(int from, int to) {
return (int) Math.floor(
(prefixSum[to] - prefixSum[from - 1]) * 1.0
/ (to - from + 1) + 0.5);
}
private static void dynamicProgramming(int count, int s) {
for (int i = 0; i <= count; i++) {
for (int j = 0; j <= s; j++) {
dp[i][j] = INF;
}
}
dp[0][0] = 0;
for (int i = 1; i <= s; ++i) {
for (int j = 1; j <= count; ++j) {
for (int k = 0; k <= j - 1; ++k) {
dp[j][i] = Math.min(dp[k][i - 1] + sumOfSquares[k + 1][j], dp[j][i]);
}
}
}
System.out.println(dp[count][s]);
}
}