-
Notifications
You must be signed in to change notification settings - Fork 0
/
10172.cpp
62 lines (51 loc) · 1.34 KB
/
10172.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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ll t; cin >> t;
while(t--){
ll n,s,q; cin >> n >> s >> q;
queue<ll> qu[n];
ll mQ[n];
stack<ll> car;
ll sum = 0, tm = 0;
for(ll i = 0; i < n; i++){
ll temp; cin >> temp;
mQ[i]=temp;
sum += temp;
for(ll j = 0; j < temp; j++){
ll tempp; cin >> tempp;
qu[i].push(tempp-1);
}
}
ll sitr = 0;
while(sum>0){
while(!car.empty()){ // unload
if(car.top() == sitr){
--sum;
}
else if(qu[sitr].size() == q){
break;
}
else{
qu[sitr].push(car.top());
}
++tm;
car.pop();
}
while(car.size() < s && !qu[sitr].empty()){ // load
car.push(qu[sitr].front());
qu[sitr].pop();
++tm;
}
if(sum!=0) tm += 2;
sitr = (sitr+1)%n;
}
cout << tm << "\n";
}
return 0;
}