-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_10172.cpp
98 lines (90 loc) · 1.68 KB
/
prob_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
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
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <climits>
#include <list>
#include <forward_list>
#define rep(i,a,b) for(i=a;i<b;++i)
#define vectIter(vect) for(auto &val: vect)
#define mapIter(map_) for(auto &val_: map_)
#define gc getchar_unlocked
using namespace std;
#define SIZE 1000005
char command[10];
void read_string(){
int i=0;
char ch;
while((ch=gc())=='\n');
while(ch!='\n' && ch!=' ')
command[i++]=ch,ch=gc();
command[i]='\0';
}
int read_int(){
int a=0;
char ch;
while((ch=gc())=='\n');
while(ch!='\n' && ch!=' ')
a=(a<<1)+(a<<3)+ch-48,
ch=gc();
return a;
}
bool check_if_unloaded(queue<int> stations[105], int N){
int i;
rep(i,1,N+1){
if(!stations[i].empty())
return false;
}
return true;
}
int main(){
int set,N,S,Q;
int i,j,k;
set= read_int();
queue<int> stations[105];
stack<int> carrier;
while(set--){
int time=0;
bool flag=true;
scanf("%d%d%d",&N,&S,&Q);
rep(i,1,N+1){
k= read_int();
rep(j,0,k)
stations[i].push(read_int());
}
while(flag){
rep(i,1,N+1){
while(!carrier.empty()){
if(carrier.top()!=i){
if(stations[i].size()<Q)
stations[i].push(carrier.top());
else break;
}
carrier.pop();
++time;
}
while(carrier.size()<S && !stations[i].empty()){
carrier.push(stations[i].front());
stations[i].pop();
++time;
}
if(!check_if_unloaded(stations,N) || !carrier.empty())
time+=2;
else{
flag= false;
break;
}
}
}
cout << time <<endl;
}
}