-
Notifications
You must be signed in to change notification settings - Fork 7
/
1160 Post Office.java
51 lines (50 loc) · 1.32 KB
/
1160 Post Office.java
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
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int villageNum = in.nextInt();
int postNum = in.nextInt();
int[][] dp = new int[villageNum][postNum];
int[][] dis = new int[villageNum][villageNum];
int[] input = new int[villageNum];
for (int i = 0; i < villageNum; ++i) {
input[i] = in.nextInt();
}
if (postNum >= villageNum) {
System.out.println("0");
} else {
init(dis, input);
for (int i = 0; i < villageNum; ++i) {
Arrays.fill(dp[i], Integer.MAX_VALUE / 2);
}
for (int i = 0; i < villageNum; ++i) {
dp[i][0] = dis[0][i];
}
for (int k = 1; k < postNum; ++k) {
for (int i = 0; i < villageNum; ++i) {
for (int j = 0; j < i; ++j) {
if (dp[j][k - 1] + dis[j + 1][i] < dp[i][k]) {
dp[i][k] = dp[j][k - 1] + dis[j + 1][i];
}
}
}
}
System.out.println(dp[villageNum - 1][postNum - 1]);
}
}
private static void init(int[][] dis, int[] input) {
for (int i = 0; i < dis.length - 1; ++i) {
for (int j = i + 1; j < dis.length; ++j) {
int mid = (i + j) / 2;
for (int k = i; k < mid; ++k) {
dis[i][j] += input[mid] - input[k];
}
for (int k = mid + 1; k <= j; ++k) {
dis[i][j] += input[k] - input[mid];
}
}
}
}
}