-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubly liked list_soldier.cpp
234 lines (201 loc) · 5.18 KB
/
doubly liked list_soldier.cpp
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
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#define CMD_INIT 1
#define CMD_HIRE 2
#define CMD_FIRE 3
#define CMD_UPDATE_SOLDIER 4
#define CMD_UPDATE_TEAM 5
#define CMD_BEST_SOLDIER 6
extern void init();
extern void hire(int mID, int mTeam, int mScore);
extern void fire(int mID);
extern void updateSoldier(int mID, int mScore);
extern void updateTeam(int mTeam, int mChangeScore);
extern int bestSoldier(int mTeam);
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
static bool run()
{
int numQuery;
int mID, mTeam, mScore, mChangeScore;
int userAns, ans;
bool isCorrect = false;
scanf("%d", &numQuery);
for (int i = 0; i < numQuery; ++i)
{
int cmd;
scanf("%d", &cmd);
switch (cmd)
{
case CMD_INIT:
init();
isCorrect = true;
break;
case CMD_HIRE:
scanf("%d %d %d", &mID, &mTeam, &mScore);
hire(mID, mTeam, mScore);
break;
case CMD_FIRE:
scanf("%d", &mID);
fire(mID);
break;
case CMD_UPDATE_SOLDIER:
scanf("%d %d", &mID, &mScore);
updateSoldier(mID, mScore);
break;
case CMD_UPDATE_TEAM:
scanf("%d %d", &mTeam, &mChangeScore);
updateTeam(mTeam, mChangeScore);
break;
case CMD_BEST_SOLDIER:
scanf("%d", &mTeam);
userAns = bestSoldier(mTeam);
scanf("%d", &ans);
if (userAns != ans)
{
isCorrect = false;
}
break;
default:
isCorrect = false;
break;
}
}
return isCorrect;
}
int main()
{
setbuf(stdout, NULL);
freopen("sample_input.txt", "r", stdin);
int T, MARK;
scanf("%d %d", &T, &MARK);
for (int tc = 1; tc <= T; tc++)
{
int score = run() ? MARK : 0;
printf("#%d %d\n", tc, score);
}
return 0;
}
const int MIN_ID = 1;
const int MAX_ID = 100000;
const int MIN_TEAM = 1;
const int MAX_TEAM = 5;
const int MIN_SCORE = 1;
const int MAX_SCORE = 5;
struct Node
{
int id;
int team;
Node *prev;
Node *next;
};
struct List
{
Node *head;
Node *tail;
//member method
static void link(Node *front, Node *back)
{
front->next = back;
back->prev = front;
}
//member method
static void erase(Node *node)
{
link(node->prev, node->next);
}
//Link garbage head and tail for empty list.
void initialize()
{
head = new Node();
tail = new Node();
link(head, tail);
}
void insert(Node *node)
{
link(tail->prev, node);
link(node, tail);
}
bool isEmpty()
{
return (head->next == tail);
}
//Add a list to the current list.
void splice(List *list)
{
if (list->isEmpty())
return;
link(tail->prev, list->head->next);
link(list->tail->prev, tail);
list->initialize();
}
};
Node soldier[MAX_ID + 1];
List soldierGroup[MAX_TEAM + 1][MAX_SCORE + 1];
void init()
{
for (int i = MIN_TEAM; i <= MAX_TEAM; i++)
for (int j = MIN_SCORE; j <= MAX_SCORE; j++)
soldierGroup[i][j].initialize();
}
void hire(int mID, int mTeam, int mScore)
{
soldier[mID].id = mID;
soldier[mID].team = mTeam;
soldierGroup[mTeam][mScore].insert(&soldier[mID]);
}
void fire(int mID)
{
List::erase(&soldier[mID]);
}
void updateSoldier(int mID, int mScore)
{
List::erase(&soldier[mID]);
soldierGroup[soldier[mID].team][mScore].insert(&soldier[mID]);
}
void updateTeam(int mTeam, int mChangeScore)
{
if (mChangeScore > 0)
{
for (int i = MAX_SCORE; i >= MIN_SCORE; i--)
{
int newScore = i + mChangeScore;
if (newScore > MAX_SCORE)
newScore = MAX_SCORE;
if(newScore != i) soldierGroup[mTeam][newScore].splice(&soldierGroup[mTeam][i]);
}
}
else if (mChangeScore < 0)
{
for (int i = MIN_SCORE; i <= MAX_SCORE; i++)
{
int newScore = i + mChangeScore;
if (newScore < MIN_SCORE)
newScore = MIN_SCORE;
if(newScore != i) soldierGroup[mTeam][newScore].splice(&soldierGroup[mTeam][i]);
}
}
}
int bestSoldier(int mTeam)
{
List maxScoreGroup;
for (int i = MAX_SCORE; i >= MIN_SCORE; i--)
{
if (!soldierGroup[mTeam][i].isEmpty())
{
maxScoreGroup = soldierGroup[mTeam][i];
break;
}
}
int maxId = MIN_ID - 1;
Node *maxScoreSoldier = maxScoreGroup.head->next;
while (maxScoreSoldier != (maxScoreGroup.tail))
{
if (maxId < maxScoreSoldier->id)
maxId = maxScoreSoldier->id;
maxScoreSoldier = maxScoreSoldier->next;
}
return maxId;
}