-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMegacity.java
55 lines (51 loc) · 1.4 KB
/
Megacity.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
/** https://codeforces.com/problemset/problem/424/B #binary-search #greedy */
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Megacity {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int s = sc.nextInt();
ArrayList<City> arrList = new ArrayList<>();
int x, y, population;
for (int i = 0; i < n; i++) {
x = sc.nextInt();
y = sc.nextInt();
population = sc.nextInt();
arrList.add(new City(x, y, population));
}
Collections.sort(arrList);
int target = 1000000 - s;
ArrayList<Integer> sumArrList = new ArrayList<>();
sumArrList.add(arrList.get(0).population);
int curSum = 0;
int idx = -1;
for (int i = 0; i < arrList.size(); i++) {
curSum += arrList.get(i).population;
if (curSum >= target) {
idx = i;
break;
}
}
if (idx == -1) {
System.out.println(-1);
} else {
x = arrList.get(idx).x;
y = arrList.get(idx).y;
double val = Math.sqrt(x * x + y * y);
System.out.println(val);
}
}
}
class City implements Comparable<City> {
int x, y, population;
City(int x, int y, int population) {
this.x = x;
this.y = y;
this.population = population;
}
public int compareTo(City other) {
return (x * x + y * y - (other.x * other.x + other.y * other.y));
}
}