-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcpc.java
260 lines (231 loc) · 8.98 KB
/
gcpc.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
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
import java.io.*;
public class Gcpc {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
String[] details = bf.readLine().split(" ");
int teams = Integer.parseInt(details[0]);
int events = Integer.parseInt(details[1]);
OwnAVLTree avlTree = new OwnAVLTree();
Team[] teamsArr = new Team[teams + 1];
for (int i = 1; i < teams + 1; i++) { //team start with 1
teamsArr[i] = new Team(i);
}
avlTree.insert(teamsArr[1]); // for comparison
for (int i = 0; i < events; i++) {
details = bf.readLine().split(" ");
int teamNum = Integer.parseInt(details[0]);
int penalty = Integer.parseInt(details[1]);
avlTree.delete(teamsArr[teamNum]);
teamsArr[teamNum].solvedProblem(penalty);
avlTree.insert(teamsArr[teamNum]);
//only need to print the rank of team 1
writer.println(avlTree.getRank(teamsArr[1]));
}
writer.flush();
writer.close();
bf.close();
}
}
class Team implements Comparable<Team> {
int teamNum;
int numSolvedProblems;
int penalty;
public Team(int teamNum) {
this.teamNum = teamNum;
this.numSolvedProblems = 0; //at first all team solve 0 prob
this.penalty = 0; // at first all team penalty is 0
}
// increment numSolvedProblems and add penalty
public void solvedProblem(int penalty) {
this.numSolvedProblems++;
this.penalty += penalty;
}
@Override
public int compareTo(Team other) {
if(this.numSolvedProblems > other.numSolvedProblems){
return -1;
} else if(this.numSolvedProblems < other.numSolvedProblems) {
return 1;
} else { //if equal check penalty
if(this.penalty > other.penalty) {
return 1;
} else if(this.penalty < other.penalty) {
return -1;
} else { //same penalty return smaller team
return this.teamNum - other.teamNum; //(Integer.compare(this.teamNum, other.teamNum)
}
}
}
}
class AVLVertex { //node class for my AVL tree
public AVLVertex parent, left, right;
public Team key; //data for each node will be the team information
public int height;
public int size;
AVLVertex(Team t) {
key = t;
parent = left = right = null;
height = 0;
size = 1;
}
public void updateInfo() {
if(this.left == null && this.right == null){
this.height = 0;
this.size = 1;
} else {
if(this.left == null) { // no left child
this.height = this.right.height + 1;
this.size = this.right.size + 1;
} else if(this.right == null) { //no right child
this.height = this.left.height + 1;
this.size = this.left.size + 1;
} else {
this.height = Math.max(this.left.height, this.right.height) + 1;
this.size = this.left.size + this.right.size + 1;
}
}
}
}
class OwnAVLTree {
public AVLVertex root;
public OwnAVLTree() { this.root = null; }
public Team findMin(AVLVertex T) {
if (T.left == null) {
return T.key; // this is the lowest ranked team
}
else {
return findMin(T.left); // go to the left recursively
}
}
// public method called to insert a new key with value v into BST
public void insert(Team team) { root = insert(root, team); }
// helper recursive method to perform insertion of new vertex into BST
protected AVLVertex insert(AVLVertex T, Team team) {
if (T == null) {
return new AVLVertex(team); // insertion point is found
} else if (T.key.compareTo(team) >= 0) { // search to the left : team < T
T.left = insert(T.left, team);
T.left.parent = T;
} else {
T.right = insert(T.right, team); // search to the right
T.right.parent = T;
}
T.updateInfo();
T = balance(T); // need to balance since its AVL
return T; // return the updated AVL
}
// public method to delete a vertex containing key with value v from BST
public void delete(Team team) { root = delete(root, team); }
// helper recursive method to perform deletion
protected AVLVertex delete(AVLVertex T, Team team) { //protected not used by the user
if(T == null) { return null; }
if (T.key.compareTo(team) > 0) {
T.left = delete(T.left, team); // search to the left
} else if (T.key.compareTo(team) < 0) { // search to the right
T.right = delete(T.right, team);
} 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
Team successorV = findMin(T.right);
T.key = successorV; // replace this key with the successor's key
T.right = delete(T.right, successorV); // delete the old successorV
}
}
if(T != null) {
T.updateInfo();
T = balance(T); // need to balance since its AVL
}
return T; // return the updated BST
}
public AVLVertex leftRotate(AVLVertex T) {
if(T == null) {
return null;
}
AVLVertex w = T.right;
w.parent = T.parent;
T.parent = w;
T.right = w.left;
if (w.left != null) {
w.left.parent = T;
}
w.left = T; // Update the height of T and then w
T.updateInfo();
w.updateInfo();
return w;
}
//mirror of the leftRotate
public AVLVertex rightRotate(AVLVertex T) {
if(T == null) {
return null;
}
AVLVertex w = T.left;
w.parent = T.parent;
T.parent = w;
T.left = w.right;
if (w.right != null) {
w.right.parent = T;
}
w.right = T; // Update the height of T and then w
T.updateInfo();
w.updateInfo();
return w;
}
public int getBalanceFactor(AVLVertex T) {
if(T == null) {
return 0;
} else {
int leftHeight = -1;
int rightHeight = -1;
if(T.left != null) { //must check for null in case no left child
leftHeight = T.left.height;
} // LOGIC ERROR HERE (ELSEIF WILL NOT TRIGGER IF "IF" TRIGGERS) - SPLIT THE CONDITIONS
if(T.right != null) { //must check for null in case no right child
rightHeight = T.right.height;
}
return leftHeight - rightHeight;
}
}
public AVLVertex balance(AVLVertex T){
int balanceFactor = getBalanceFactor(T);
if(balanceFactor == 2) { //left something case
if(getBalanceFactor(T.left) == -1) { //left right case
T.left = leftRotate(T.left);
}
T = rightRotate(T); //left left case and left right case both need to rightRotate(T)
} else if(balanceFactor == -2) { //right something case
if(getBalanceFactor(T.right) == 1) { //right left case
T.right = rightRotate(T.right);
}
T = leftRotate(T); //right right case and right left case both need to leftRotate(T)
}
return T;
}
//the rank of the team would be : total team - number of team lower ranked then this team + 1
public int getRank(Team team) {
return rank(this.root, team);
}
protected int rank(AVLVertex T, Team team) {
int leftSize = 0;
if(T.key.compareTo(team) < 0) { // team is on the right
if(T.left != null) { //need to check null
leftSize = T.left.size;
}
return rank(T.right, team) + leftSize + 1;
} else if(T.key.compareTo(team) > 0) { //team is on the left
return rank(T.left, team);
} else { // team is equals
if(T.left != null) { //need to check null
leftSize = T.left.size;
}
return leftSize + 1;
}
}
}