-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva10172.cpp
More file actions
81 lines (66 loc) · 1.9 KB
/
uva10172.cpp
File metadata and controls
81 lines (66 loc) · 1.9 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
/* Rishikesh
* UVA 10172
* Read the problem statement very very carefully
*/
#include<iostream>
#include<queue>
#include<stack>
#include<string>
#include<sstream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
string s;
int ninputs;
cin >> ninputs;
while (ninputs--) {
int N, S, Q;
cin >> N >> S >> Q;
vector<queue<int>> qB(N);
int totalCargo = 0;
for (int i = 0; i< N;i++) {
int nC; cin >> nC;
totalCargo += nC;
for(int j = 0; j < nC; j++) {
int tmp;
cin >> tmp;
qB[i].push(tmp-1);
}
}
stack<int> truck;
int currentPosition = 0;
int timetaken=0;
int timeToUnload = 1;
int timeToLoad = 1;
int travelTime = 2;
int queueCapacity = Q;
int truckCapacity = S;
while(totalCargo) {
//Unload
while (not truck.empty() and
(truck.top() == currentPosition
or qB[currentPosition].size() < queueCapacity)) {
if (truck.top() == currentPosition) {
totalCargo--; //amount of work remaining is decreased
} else {
qB[currentPosition].push(truck.top());
}
truck.pop();
timetaken += timeToUnload;
}
//Load
while(truck.size() < truckCapacity and
not qB[currentPosition].empty()) {
truck.push(qB[currentPosition].front());
qB[currentPosition].pop();
timetaken += timeToLoad;
}
//Go to next position
currentPosition = (currentPosition + 1) % N;
if (totalCargo)
timetaken += travelTime;
}
cout << timetaken << endl;
}
}