-
Notifications
You must be signed in to change notification settings - Fork 0
/
2000_S4.java
43 lines (38 loc) · 1.24 KB
/
2000_S4.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
//Dynamic programming
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
int distance = readInt();
int num = readInt();
int[] clubs = new int[num];
for (int i = 0; i < num; i++) clubs[i] = readInt();
int[] least = new int[distance + 1]; Arrays.fill(least, 5281);
least[0] = 0;
for (int i = 0; i < least.length; i++){
for (int j = 0; j < clubs.length; j++){
if (i + clubs[j] <= distance){
if (least[i] + 1 < least[i + clubs[j]]){
least[i + clubs[j]] = least[i] + 1;
}
}
}
}
if (least[distance] >= 5281) System.out.println("Roberta acknowledges defeat.");
else System.out.printf("Roberta wins in %d strokes.", least[distance]);
}
//fast i/o
static StringTokenizer st;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static String next () throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine().trim());
return st.nextToken();
}
static int readInt () throws IOException {
return Integer.parseInt(next());
}
static String readLine () throws IOException {
return br.readLine().trim();
}
}