-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path10172-The Lonesome Cargo Distributor.cpp
70 lines (69 loc) · 1.7 KB
/
10172-The Lonesome Cargo Distributor.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
using namespace std;
int main() {
int t, n, s, q;
int c, v;
scanf("%d\n",&t);
while(t--){
cin >> n >> s >> q;
int time = 0;
int cur = 0;
int remaining = 0;
stack<int> carrier;
vector<queue<int>> stations(n,queue<int>());
for(int i=0;i<n;i++){
cin >> c;
remaining += c;
while(c--){
cin >> v;
stations[i].push(v);
}
}
while(remaining){
// upload cargo
while(!carrier.empty()){
// unload to A
if(carrier.top() == cur+1) {
carrier.pop();
time++;
remaining--;
}
// attempt unload to B
else if(stations[cur].size() >= q) break;
else {
stations[cur].push(carrier.top());
carrier.pop();
time++;
}
}
// load to carrier
while(!stations[cur].empty() && carrier.size() < s){
carrier.push(stations[cur].front());
stations[cur].pop();
time++;
}
if(remaining){
cur = (cur+1)%n;
time += 2;
}
}
cout << time << endl;
}
}