/
10800.java
129 lines (109 loc) · 2.43 KB
/
10800.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Main {
private void solve() {
// https://mygumi.tistory.com/273
int n = sc.nextInt();
ArrayList<Ball> input = new ArrayList<Ball>();
int[] players = new int[200001];
int[] colors = new int[200001];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
int color = sc.nextInt();
int size = sc.nextInt();
input.add(new Ball(i, color, size));
}
Collections.sort(input, new Comparator<Ball>() {
public int compare(Ball b1, Ball b2) {
if (b1.size < b2.size) {
return -1;
} else if (b1.size > b2.size) {
return 1;
} else {
if (b1.color < b2.color) {
return -1;
} else {
return 1;
}
}
}
});
int sum = 0;
for (int i = 0, j = 0; i < n; i++) {
Ball a = input.get(i);
Ball b = input.get(j);
while (b.size < a.size) {
sum += b.size;
colors[b.color] += b.size;
b = input.get(++j);
}
players[a.id] = sum - colors[a.color];
}
for (int i = 0; i < n; i++) {
sb.append(players[i] + "\n");
}
System.out.println(sb.toString());
}
public static class Ball {
int id;
int color;
int size;
Ball(int id, int color, int size) {
this.id = id;
this.color = color;
this.size = size;
}
}
public static void main(String[] args) {
sc.init();
new Main().solve();
}
static class sc {
private static BufferedReader br;
private static StringTokenizer st;
static void init() {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
static String readLine() {
try {
return br.readLine();
} catch (IOException e) {
}
return null;
}
static String readLineReplace() {
try {
return br.readLine().replaceAll("\\s+", "");
} catch (IOException e) {
}
return null;
}
static String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
}
}
return st.nextToken();
}
static boolean hasNext() {
return st.hasMoreTokens();
}
static long nextLong() {
return Long.parseLong(next());
}
static int nextInt() {
return Integer.parseInt(next());
}
static double nextDouble() {
return Double.parseDouble(next());
}
}
}