-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCPC.java
More file actions
555 lines (464 loc) · 17.3 KB
/
GCPC.java
File metadata and controls
555 lines (464 loc) · 17.3 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.*;
// barebones implementation of BST
// Every vertex in this BST is a Java Class
class BSTVertex {
// all these attributes remain public to slightly simplify the code
public BSTVertex parent, left, right;
public Team key;
public int height; // will be used in lecture on AVL
public int size; // will be used in lecture on AVL
BSTVertex(Team v) {
key = v;
parent = left = right = null;
height = 0;
size = 1; }
public boolean equalsTo(BSTVertex r) {
return ((this.key.teamID == r.key.teamID));
}
}
// This is just a sample implementation
// There are other ways to implement BST concepts...
class AVL {
public BSTVertex root;
public AVL() { root = null; }
public int size(BSTVertex N) {
if (N == null) {
return 0;
}
return N.size;
}
public int height(BSTVertex N) {
if (N == null)
return -1;
return N.height;
}
public int max(int a , int b) {
if (a > b) {
return a;
} else {
return b;
}
}
public int rank(Team v) {
return rank(root, v);
}
public int rank(BSTVertex T, Team v) {
if (T == null) {
return 0;
}
int comp = v.compareTo(T.key);
if (comp < 0) {
return rank(T.left,v);
} else if (comp > 0) {
return 1 + size(T.left) + rank(T.right,v);
} else if (T.key.equalsTo(v)) {
return size(T.left) + 1;
}
return 0;
}
public boolean contains(Team v) {
return search(v) != null;
}
public BSTVertex leftRotate(BSTVertex T) {
BSTVertex w = T.right;
if (T.right != null) {
w.parent = T.parent;
T.parent = w;
T.right = w.left;
if (w.left != null) {
w.left.parent = T;
}
w.left = T;
T.height = max(height(T.left), height(T.right)) + 1;
w.height = max(height(w.left), height(w.right)) + 1;
T.size = size(T.left) + size(T.right) + 1;
w.size = size(w.left) + size(w.right) + 1;
}
return w;
}
public BSTVertex rightRotate(BSTVertex T) {
BSTVertex w = T.left;
if (T.left != null) {
w.parent = T.parent;
T.parent = w;
T.left = w.right;
if (w.right != null) {
w.right.parent = T;
}
w.right = T;
T.height = max(height(T.left), height(T.right)) + 1;
w.height = max(height(w.left), height(w.right)) + 1;
T.size = size(T.left) + size(T.right) + 1;
w.size = size(w.left) + size(w.right) + 1;
}
return w;
}
public int getBalance(BSTVertex N) {
if (N == null)
return 0;
return height(N.left) - height(N.right);
}
// public method called to search for a value v.
// Return v if it is found in the BST otherwise return -1.
// Here the assumption is that -1 is never a valid key value.
public Team search (Team v) { //need to call search using team v and the same teamID
BSTVertex res = search(root, v);
return res == null ? null : res.key;
}
// helper method to perform search
public BSTVertex search(BSTVertex T, Team v) {
if (T == null) return null; // not found
else if ((T.key.teamID == v.teamID)) return T; // found
else if (T.key.compareTo(v) < 0) return search(T.right, v); // search to the right
else return search(T.left, v); // search to the left
}
// public method called to find Minimum key value in BST
public BSTVertex findMin() { return findMin(root); }
// helper method to perform findMin
// Question: What happens if BST is empty?
public BSTVertex findMin(BSTVertex T) {
if (T.left == null) return T; // this is the min
else return findMin(T.left); // go to the left
}
// public method called to find Maximum key value in BST
public Team findMax() { return findMax(root); }
// helper method to perform findMax
// Question: Again, what happens if BST is empty?
public Team findMax(BSTVertex T) {
if (T.right == null) return T.key; // this is the max
else return findMax(T.right); // go to the right
}
// public method to find successor to given value v in BST.
public BSTVertex successor(Team v) {
BSTVertex vPos = search(root, v);
return vPos == null ? null : successor(vPos);
}
// helper recursive method to find successor to for a given vertex T in BST
public BSTVertex successor(BSTVertex T) {
if (T.right != null) // this subtree has right subtree
return findMin(T.right); // the successor is the minimum of right subtree
else {
BSTVertex par = T.parent;
BSTVertex cur = T;
// if par(ent) is not root and cur(rent) is its right children
while ((par != null) && (cur.equalsTo(par.right))) {
cur = par; // continue moving up
par = cur.parent;
}
return par == null ? null : par; // this is the successor of T
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
// public method to find predecessor to given value v in BST
public Team predecessor(Team v) {
BSTVertex vPos = search(root, v);
return vPos == null ? null : predecessor(vPos);
}
// helper recursive method to find predecessor to for a given vertex T in BST
public Team predecessor(BSTVertex T) {
if (T.left != null) // this subtree has left subtree
return findMax(T.left); // the predecessor is the maximum of left subtree
else {
BSTVertex par = T.parent;
BSTVertex cur = T;
// if par(ent) is not root and cur(rent) is its left children
while ((par != null) && (cur.equalsTo(par.left))) {
cur = par; // continue moving up
par = cur.parent;
}
return par == null ? null : par.key; // this is the successor of T
}
}
// public method called to perform inorder traversal
public void inorder() {
inorder(root);
System.out.println();
}
// helper method to perform inorder traversal
public void inorder(BSTVertex T) {
if (T == null) return;
inorder(T.left); // recursively go to the left
System.out.println(T.key); // visit this BST node
inorder(T.right); // recursively go to the right
}
// public method called to perform preorder traversal
public void preorder() {
preorder(root);
System.out.println();
}
// helper method to perform preorder traversal
public void preorder(BSTVertex T) {
if (T == null) return;
System.out.println(T.key); // visit this BST node
inorder(T.left); // recursively go to the left
inorder(T.right); // recursively go to the right
}
// public method called to insert a new key with value v into BST
public void insert(Team v) { root = insert(root, v); }
// helper recursive method to perform insertion of new vertex into BST
public BSTVertex insert(BSTVertex T, Team v) {
if (T == null) return new BSTVertex(v); // insertion point is found
if (v.compareTo(T.key) > 0) { // search to the right
T.right = insert(T.right, v);
T.right.parent = T;
}
else if (v.compareTo(T.key) < 0) { // search to the left
T.left = insert(T.left, v);
T.left.parent = T;
} else if (T.key.teamID == v.teamID) {
return T;
}
T.height = 1 + max(height(T.left),height(T.right));
T.size = 1 + size(T.left) + size(T.right);
int balance = getBalance(T);
if (balance > 1) {
int leftBalanceFactor = getBalance(T.left);
if (leftBalanceFactor < 0) {
//left right case
T.left = leftRotate(T.left);
return rightRotate(T);
} else {
//left left case
return rightRotate(T);
}
}
if (balance < -1) {
int rightBalanceFactor = getBalance(T.right);
if (rightBalanceFactor <= 0) {
//right right case
return leftRotate(T);
} else {
// right left case
T.right = rightRotate(T.right);
return leftRotate(T);
}
}
return T; // return the updated BST
}
// public method to delete a vertex containing key with value v from BST
public void delete(Team v) { root = delete(root, v); }
// helper recursive method to perform deletion
public BSTVertex delete(BSTVertex T, Team v) {
if (T == null) return T; // cannot find the item to be deleted
if (v.compareTo(T.key) > 0) // search to the right
T.right = delete(T.right, v);
else if (v.compareTo(T.key) < 0) // search to the left
T.left = delete(T.left, v);
else { // this is the node to be deleted
if (T.left == null && T.right == null) // this is a leaf
T = null; // simply erase this node
else if (T.left == null && T.right != null) { // only one child at right
T.right.parent = T.parent;
T = T.right; // bypass T
}
else if (T.left != null && T.right == null) { // only one child at left
T.left.parent = T.parent;
T = T.left; // bypass T
}
else { // has two children, find successor
BSTVertex successorV = successor(v);
T.key = successorV.key; // replace this key with the successor's key
T.right = delete(T.right, successorV.key); // delete the old successorV
// T.left = successorV.left;
}
}
if (T == null) {
return T;
}
T.height = max(height(T.left), height(T.right)) + 1;
T.size = 1 + size(T.left) + size(T.right);
int balance = getBalance(T);
if (balance > 1) {
int leftBalanceFactor = getBalance(T.left);
if (leftBalanceFactor < 0) {
//left right case
T.left = leftRotate(T.left);
return rightRotate(T);
} else {
//left left case
return rightRotate(T);
}
}
if (balance < -1) {
int rightBalanceFactor = getBalance(T.right);
if (rightBalanceFactor <= 0) {
//right right case
return leftRotate(T);
} else {
// right left case
T.right = rightRotate(T.right);
return leftRotate(T);
}
}
return T; // return the updated BST
}
}
class Team implements Comparable<Team> {
public int teamID;
public int penalty;
public int score;
public Team(int teamID, int penalty) {
this.teamID = teamID;
this.penalty = penalty;
this.score = 1;
}
public void updateWith(Team t) { // to update the team that is currently inside
this.penalty += t.penalty;
this.score += t.score;
}
public boolean equalsTo(Team t) {
return this.teamID == t.teamID && this.penalty == t.penalty && this.score == t.score;
}
@Override
public int compareTo(Team t2) {
if (this.score != t2.score) {
return t2.score - this.score;
} else if (this.penalty != t2.penalty) {
return this.penalty - t2.penalty;
} else {
return this.teamID - t2.teamID;
}
}
@Override
public String toString() {
return "this is team "+ teamID+" with total penalty at "+penalty+" and score of "+score;
}
}
public class GCPC {
public static void main(String[] args) throws Exception {
FastIO fio = new FastIO();
Hashtable<Integer, Team> h = new Hashtable<Integer, Team>();
AVL avl = new AVL(); // an empty BST
int numberOfTeams = fio.nextInt();
int numberOfEvents = fio.nextInt();
for(int i = 0; i < numberOfEvents; i++) {
int teamNum = fio.nextInt();
int penalty = fio.nextInt();
Team team = new Team(teamNum,penalty);
if (h.containsKey(teamNum)){
Team oldTeam = h.get(teamNum);
avl.delete(oldTeam);
team.updateWith(oldTeam);
avl.insert(team);
h.remove(teamNum);
h.put(teamNum,team);
} else {
h.put(teamNum,team);
avl.insert(team);
}
// fio.println("THIS IS THE NEW TEAM CREATED N UPDATED: " + team);
// avl.inorder();
if (h.containsKey(1)){
Team teamToSearch = h.get(1);
int position = avl.rank(teamToSearch);
fio.println(position);
} else {
// print size of root + 1
int position = h.size();
fio.println(position + 1);
}
//make new team with teamNum
// search for teamNum
// if null --> insert into bst
// else
// update the new team
// delete the node
// insert into bst
// end
//search team 1
// if null, print size of root + 1
// else print rank(team 1)
}
// // Sample BST as shown in Lecture
// T.insert(15);
// T.insert(23);
// T.insert(6);
// T.insert(71);
// T.insert(50);
// T.insert(4);
// T.insert(7);
// T.insert(5);
// System.out.println(T.search(71)); // found, 71
// System.out.println(T.search(7)); // found, 7
// System.out.println(T.search(22)); // not found, -1
// System.out.println(T.findMin()); // 4
// System.out.println(T.findMax()); // 71
// System.out.println(T.successor(23)); // 50
// System.out.println(T.successor(7)); // 15
// System.out.println(T.successor(71)); // -1
// System.out.println(T.predecessor(23)); // 15
// System.out.println(T.predecessor(7)); // 6
// System.out.println(T.predecessor(71)); // 50
// T.inorder(); // The BST: 4, 5, 6, 7, 15, 23, 50, 71
// System.out.println("Deleting 5");
// T.delete(5);
// System.out.println("Deleting 71");
// T.delete(71);
// System.out.println("Deleting 15");
// T.delete(15);
// T.inorder();
// avl.inorder(); // The BST: 4, 6, 7, 23, 50
fio.close();
}
}
/**
* Fast I/O
* @source https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
*/
class FastIO extends PrintWriter
{
BufferedReader br;
StringTokenizer st;
public FastIO()
{
super(new BufferedOutputStream(System.out));
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}