-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJennyBatteries.java
More file actions
85 lines (84 loc) · 2 KB
/
JennyBatteries.java
File metadata and controls
85 lines (84 loc) · 2 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
80
81
82
83
84
85
import java.io.*;
import java.util.*;
public class JennyBatteries {
static int[][] batteries;
static long budget;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp = br.readLine().split(" ");
int N = Integer.parseInt(temp[0]);
budget = Integer.parseInt(temp[1]);
batteries = new int[N][3];
for (int i = 0; i < N; i++)
{
temp = br.readLine().split(" ");
batteries[i][0] = Integer.parseInt(temp[0]);
batteries[i][1] = Integer.parseInt(temp[1]);
batteries[i][2] = Integer.parseInt(temp[2]);
}
Comparator<int[]> c = new Comparator<int[]>()
{
public int compare(int[] o1, int[] o2)
{
return o1[2] - o2[2]; //ascending order of c value
}
};
Arrays.sort(batteries, c);
long start = batteries[0][0];
long end = start;
for (int i = 0; i < N; i++)
{
if (batteries[i][0] > end)
end = batteries[i][0];
if (batteries[i][0] < start)
start = batteries[i][0];
}
long med;
boolean pass;
while (start != end)
{
med = (start + end)/2;
pass = pass(med);
//System.out.println(med + " " + pass);
if (pass)
end = med;
else
start = med + 1;
}
System.out.println(start);
br.close();
}
public static boolean pass(long med)
{
long count = 0;
long cost = 0;
//System.out.println("TEST: " + med);
for (int[] element: batteries)
{
if (element[0] > med)
{
count += element[0] - med;
cost += (element[0]-med)*element[1];
}
}
//System.out.println("Count: " + count);
int i = 0;
while (i < batteries.length && count > 0)
{
if (batteries[i][0] >= med)
{
i++;
continue;
}
//System.out.println(batteries[i][0] + " " + batteries[i][2]);
count -= Math.min(count, (med - batteries[i][0]));
cost += Math.min(count, (med - batteries[i][0])) * batteries[i][2];
i++;
}
//System.out.println(med + " Cost: " + cost);
if (i > batteries.length && count > 0)
return false;
return cost <= budget;
}
}