-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
365 lines (302 loc) · 7.74 KB
/
Copy pathmain.cpp
File metadata and controls
365 lines (302 loc) · 7.74 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
#include <iostream>
using namespace std;
#define NULL 0
const int CMD_INIT = 1;
const int CMD_ADD = 2;
const int MAX_WAIT_SIZE = 10000;
const int MAX_MACHINE_SIZE = 100;
typedef struct _Wait {
int mId;
int mNum;
int mPriority;
bool operator>(const _Wait& rhs);
_Wait& operator=(const _Wait& rhs);
} Wait;
Wait TEMP_WAITS[MAX_WAIT_SIZE] = { 0, };
typedef struct _WaitVector {
int mCapacity;
int mSize;
int mSortedIndex;
Wait* mpWait;
_WaitVector();
Wait& operator[](const int index);
void append(const int mId, const int mNum, const int mPriority);
void sort();
void _mergeSort(const int left, const int right);
void _merge(const int left, const int mid, const int right);
} WaitVector;
typedef struct _Machine {
int mId;
int mDuration;
int mCapacity;
int mStart;
int mEnd;
int mSumWaits;
WaitVector* mpWaitVector;
bool operator>(const _Machine& rhs);
_Machine& operator=(const _Machine& rhs);
void addWait(const int mId, const int mNum, const int mPriority);
} Machine;
Machine TEMP_MACHINES[MAX_MACHINE_SIZE] = { 0, };
typedef struct _MachineVector {
int mCapacity;
int mSize;
int mSortedIndex;
Machine* mpMachine;
_MachineVector();
Machine& operator[](const int index);
void append(const int mId, const int mDuration, const int mCapacity);
void sort();
void _mergeSort(const int left, const int right);
void _merge(const int left, const int mid, const int right);
} MachineVector;
// initial
MachineVector MACHINES;
void printWaitVector(WaitVector& vector);
void printWaitVector(WaitVector* vector);
void printMachineVector(MachineVector& vector);
int main() {
int Q = 0;
int N = 0;
int m_id = 0;
int m_dur = 0;
int m_cap = 0;
int num = 0;
int priority = 0;
int cmd = 0;
cin >> Q;
for (int i = 0; i < Q; ++i) {
cin >> cmd;
if (cmd == CMD_INIT) {
cin >> N;
for (int n = 0; n < N; ++n) {
cin >> m_id >> m_dur >> m_cap;
MACHINES.append(m_id, m_dur, m_cap);
}
}
else if (cmd == CMD_ADD) {
// Nothing
}
}
printMachineVector(MACHINES);
MACHINES.sort();
printMachineVector(MACHINES);
return 0;
}
void printWaitVector(WaitVector& vector) {
cout << "WAIT Vector: " << endl;
for (int i = 0; i < vector.mSize; ++i) {
cout << "[" << i << "] id:" << vector[i].mId << ", num:" << vector[i].mNum << ", priority:" << vector[i].mPriority << endl;
}
}
void printWaitVector(WaitVector* vector) {
cout << "WAIT Vector: " << endl;
for (int i = 0; i < vector->mSize; ++i) {
cout << "[" << i << "] id:" << (*vector)[i].mId << ", num:" << (*vector)[i].mNum << ", priority:" << (*vector)[i].mPriority << endl;
}
}
void printMachineVector(MachineVector& vector) {
cout << "MACHINE Vector: " << endl;
for (int i = 0; i < vector.mSize; ++i) {
cout << "[" << i << "] id:" << vector[i].mId << ", dur:" << vector[i].mDuration << ", cap:" << vector[i].mCapacity << endl;
if (vector[i].mpWaitVector != NULL) {
printWaitVector(vector[i].mpWaitVector);
}
}
}
// _Wait Implementation
bool _Wait::operator>(const _Wait& rhs) {
if (this->mNum - rhs.mNum) {
return (this->mNum > rhs.mNum);
}
return (this->mId > rhs.mId);
}
_Wait& _Wait::operator=(const _Wait& rhs) {
this->mId = rhs.mId;
this->mNum = rhs.mNum;
this->mPriority = rhs.mPriority;
return *this;
}
// _WaitVector
_WaitVector::_WaitVector() {
this->mCapacity = 1;
this->mSize = 0;
this->mSortedIndex = 0;
if (this->mpWait != NULL) {
delete[] this->mpWait;
}
this->mpWait = new Wait[1];
}
Wait& _WaitVector::operator[](const int index) {
return this->mpWait[index];
}
void _WaitVector::append(const int mId, const int mNum, const int mPriority) {
if (this->mSize == this->mCapacity) {
this->mCapacity = this->mCapacity << 1;
Wait* temp = new Wait[this->mCapacity];
for (register int i = 0; i < this->mSize; ++i) {
temp[i] = this->mpWait[i];
}
delete[] this->mpWait;
this->mpWait = temp;
temp = NULL;
}
int index = this->mSize;
this->mpWait[index].mId = mId;
this->mpWait[index].mNum = mNum;
this->mpWait[index].mPriority = mPriority;
++this->mSize;
}
void _WaitVector::sort() {
const int right = this->mSize - 1;
if (this->mSortedIndex == 0) {
this->_mergeSort(0, right);
return;
}
this->_mergeSort(this->mSortedIndex, right);
this->_merge(0, this->mSortedIndex + 1, right);
this->mSortedIndex = right;
}
void _WaitVector::_mergeSort(const int left, const int right) {
if (left == right) {
return;
}
int mid = (left + right) >> 1;
this->_mergeSort(left, mid);
this->_mergeSort(mid + 1, right);
this->_merge(left, mid, right);
}
void _WaitVector::_merge(const int left, const int mid, const int right) {
int i = left;
int j = mid + 1;
int t_i = 0;
while (i <= mid && j <= right) {
if (this->mpWait[i] > this->mpWait[j]) {
TEMP_WAITS[t_i] = this->mpWait[i];
++t_i;
++i;
}
else {
TEMP_WAITS[t_i] = this->mpWait[j];
++t_i;
++j;
}
}
while (i <= mid) {
TEMP_WAITS[t_i] = this->mpWait[i];
++t_i;
++i;
}
while (j <= right) {
TEMP_WAITS[t_i] = this->mpWait[j];
++t_i;
++j;
}
for (register int k = 0; k < t_i; ++k) {
this->mpWait[left + k] = TEMP_WAITS[k];
}
}
// _Machine implementation
bool _Machine::operator>(const _Machine& rhs) {
if (this->mSumWaits - rhs.mSumWaits) {
return (this->mSumWaits > rhs.mSumWaits);
}
return (this->mId > rhs.mId);
}
_Machine& _Machine::operator=(const _Machine& rhs) {
this->mId = rhs.mId;
this->mDuration = rhs.mDuration;
this->mCapacity = rhs.mCapacity;
this->mStart = rhs.mStart;
this->mEnd = rhs.mEnd;
this->mSumWaits = rhs.mSumWaits;
this->mpWaitVector = rhs.mpWaitVector;
return *this;
}
void _Machine::addWait(const int mId, const int mNum, const int mPriority) {
this->mpWaitVector->append(mId, mNum, mPriority);
this->mSumWaits += mNum;
}
// _MachineVector Implementation
_MachineVector::_MachineVector() {
this->mCapacity = 1;
this->mSize = 0;
this->mSortedIndex = 0;
if (this->mpMachine != NULL) {
delete[] this->mpMachine;
}
this->mpMachine = new Machine[1];
}
Machine& _MachineVector::operator[](const int index) {
return this->mpMachine[index];
}
void _MachineVector::append(const int mId, const int mDuration, const int mCapacity) {
if (this->mSize == this->mCapacity) {
this->mCapacity = this->mCapacity << 1;
Machine* temp = new Machine[this->mCapacity];
for (register int i = 0; i < this->mSize; ++i) {
temp[i] = this->mpMachine[i];
}
delete[] this->mpMachine;
this->mpMachine = temp;
temp = NULL;
}
int index = this->mSize;
this->mpMachine[index].mId = mId;
this->mpMachine[index].mDuration = mDuration;
this->mpMachine[index].mCapacity = mCapacity;
this->mpMachine[index].mStart = 0;
this->mpMachine[index].mEnd = 0;
this->mpMachine[index].mSumWaits = 0;
this->mpMachine[index].mpWaitVector = NULL;
++this->mSize;
}
void _MachineVector::sort() {
const int right = this->mSize - 1;
if (this->mSortedIndex == 0) {
this->_mergeSort(0, right);
return;
}
this->_mergeSort(this->mSortedIndex, right);
this->_merge(0, this->mSortedIndex + 1, right);
this->mSortedIndex = right;
}
void _MachineVector::_mergeSort(const int left, const int right) {
if (left == right) {
return;
}
int mid = (left + right) >> 1;
this->_mergeSort(left, mid);
this->_mergeSort(mid + 1, right);
this->_merge(left, mid, right);
}
void _MachineVector::_merge(const int left, const int mid, const int right) {
int i = left;
int j = mid + 1;
int t_i = 0;
while (i <= mid && j <= right) {
if (this->mpMachine[i] > this->mpMachine[j]) {
TEMP_MACHINES[t_i] = this->mpMachine[i];
++t_i;
++i;
}
else {
TEMP_MACHINES[t_i] = this->mpMachine[j];
++t_i;
++j;
}
}
while (i <= mid) {
TEMP_MACHINES[t_i] = this->mpMachine[i];
++t_i;
++i;
}
while (j <= right) {
TEMP_MACHINES[t_i] = this->mpMachine[j];
++t_i;
++j;
}
for (register int k = 0; k < t_i; ++k) {
this->mpMachine[left + k] = TEMP_MACHINES[k];
}
}