forked from ckcz123/codejam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.java
More file actions
79 lines (70 loc) · 2.18 KB
/
Copy pathC.java
File metadata and controls
79 lines (70 loc) · 2.18 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
import java.io.PrintStream;
import java.util.*;
/**
* Codejam 2012 Round 3
* Problem C. Quality Food
* Only small solved.
*/
public class Main {
public String solve(Scanner scanner) {
return solveSmall(scanner);
}
class Meal {
long price, time;
public Meal(long _price, long _time) {
price=_price; time=_time;
}
}
private String solveSmall(Scanner scanner) {
long m=scanner.nextLong(), f=scanner.nextLong();
int n=scanner.nextInt();
Meal[] meals=new Meal[n];
for (int i=0;i<n;i++) {
meals[i]=new Meal(scanner.nextLong(), scanner.nextLong());
}
ArrayList<Long> list=new ArrayList<>();
list.add(0L);
for (int i=0;;i++) {
long last=i==0?f:list.get(i);
long p=Long.MAX_VALUE;
for (Meal meal: meals) {
if (meal.time>=i && meal.price<p) {
p=meal.price;
}
}
if (p==Long.MAX_VALUE) break;
list.add(last+p);
if (last+p>m) break;
}
long mx=0;
for (int i=1;i<list.size()-1;i++) {
mx=Math.max(mx, cal(list.get(i), list.get(i+1), i, i+1, m));
}
return String.valueOf(mx);
}
private long cal(long u, long v, long a, long b, long m) {
long mx=0;
for (long x=0;u*x<=m;x++) {
long l=m-u*x, y=l/v;
mx=Math.max(mx, a*x+b*y);
}
return mx;
}
public static void main(String[] args) throws Exception {
System.setOut(new PrintStream("output.txt"));
Scanner scanner=new Scanner(System.in);
int times=scanner.nextInt();
long start=System.currentTimeMillis();
for (int t=1;t<=times;t++) {
try {
System.out.println(String.format("Case #%d: %s", t, new Main().solve(scanner)));
}
catch (Throwable e) {
System.err.println("ERROR in case #"+t);
e.printStackTrace();
}
}
long end=System.currentTimeMillis();
System.err.println(String.format("Time used: %.3fs", (end-start)/1000.0));
}
}