-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJava.java
91 lines (72 loc) · 2.05 KB
/
Java.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.*;
class Apple{
int y;
int x;
String name;
public Apple(int y, int x, String name){
this.y = y;
this.x = x;
this.name = name;
}
public void print(){
System.err.println("Y: " + this.y + " X: " + this.x + " Name: " + this.name);
}
}
class Solution {
public static int binarySearch(List<Apple> apples, int row){
int start = 0;
int end = apples.size() - 1;
while(start <= end){
int mid = end - (end - start) / 2;
if(apples.get(mid).y <= row){
start = mid + 1;
}
else{
end = mid - 1;
}
}
return end;
}
public static void main(String args[]) {
List<Apple> apples = new ArrayList<Apple>();
Scanner in = new Scanner(System.in);
int N = in.nextInt();
for (int i = 0; i < N; i++) {
String name = in.next();
int r = in.nextInt();
int c = in.nextInt();
apples.add(new Apple(r, c, name));
}
apples.sort((Apple a, Apple b) -> {
if(a.y == b.y){
return Integer.compare(a.x, b.x);
}
return Integer.compare(a.y, b.y);
});
/*for(Apple a : apples){
a.print();
}*/
StringBuilder output = new StringBuilder();
boolean d = false;
int lastRow = -1;
for(int i = 0; i < apples.size(); i++){
Apple a = apples.get(i);
if(lastRow != a.y){
d = !d;
lastRow = a.y;
}
if(d){
output.append(a.name + ',');
}
else{
int last = binarySearch(apples, lastRow);
for(int _i = last; _i >= i; _i--){
output.append(apples.get(_i).name + ',');
}
i = last;
}
}
output.deleteCharAt(output.length() - 1);
System.out.println(output.toString());
}
}