-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarathon.java
54 lines (47 loc) · 1.75 KB
/
marathon.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
52
53
54
import java.io.*;
import java.util.*;
public class marathon {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("marathon.in"));
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("marathon.out")));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] x = new int[n];
int[] y = new int[n];
for(int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
x[i] = Integer.parseInt(st.nextToken());
y[i] = Integer.parseInt(st.nextToken());
}
int[][] dp = new int[k+1][n];
for(int i = 0; i < dp.length; i++) {
Arrays.fill(dp[i], -1);
}
dp[0][0] = 0;
for(int i = 0; i <= k; i++) {
for(int j = 0; j < n; j++) {
for(int l = j+1; l < n && i + l-j-1 <= k; l++) {
int nextI = i + (l-j-1);
int nextJ = l;
if(dp[nextI][nextJ]!=-1) {
dp[nextI][nextJ] = Math.min(dp[nextI][nextJ], dp[i][j] + distBetween(x, y, j, l));
}
else{
dp[nextI][nextJ] = dp[i][j] + distBetween(x, y, j, l);
}
}
}
}
pw.println(dp[k][n-1]);
pw.close();
}
public static int distBetween(int[] x, int[] y, int i, int j) {
return dist(x[i], y[i], x[j], y[j]);
}
public static int dist(int x1, int y1, int x2, int y2) {
int x = x1-x2;
int y = y1-y2;
return Math.abs(x) + Math.abs(y);
}
}