-
Notifications
You must be signed in to change notification settings - Fork 0
/
feast.java
61 lines (50 loc) · 1.45 KB
/
feast.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
55
56
57
58
59
60
61
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.*;
import java.util.Stack;
import java.util.StringTokenizer;
public class feast {
static int max, t, a, b;
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("feast.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("feast.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
t = Integer.parseInt(st.nextToken());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
max = 0;
dfs();
out.println(max);
out.close();
}
private static void dfs() {
Stack<Fullness> stack = new Stack<Fullness>();
stack.add(new Fullness(0, false));
boolean[] visited = new boolean[t + 1];
while (!stack.isEmpty()) {
Fullness fl = stack.pop();
if (fl.full <= t && !visited[fl.full]) {
visited[fl.full] = true;
max = Math.max(fl.full, max);
if (!fl.water) {
Fullness fl1 = new Fullness(fl.full / 2, true);
stack.push(fl1);
}
Fullness fl2 = new Fullness(fl.full + a, fl.water);
stack.push(fl2);
Fullness fl3 = new Fullness(fl.full + b, fl.water);
stack.push(fl3);
}
}
}
}
class Fullness {
int full;
boolean water;
public Fullness(int x, boolean w) {
full = x;
water = w;
}
}