UVa 822 - Queue and A

昨天ACM培训时开始写的,一看就有思路,建了两个结构体表示topics和员工。循环中间多加了个else一直没检查出来,错了好多次。

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
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
struct topic{
int tid,num,t0,t,dt,q_num,finished;
};
struct member{
int pid,k,work_left,last;
vector<int>pidk;
bool operator <(const member&a) const{
if(a.last==last) return a.pid>pid;
else return a.last>last;
}
};
vector<topic>topics;
vector<member>staff;
int main(){
int m,n,kase=0;
while(cin>>m&&m){
int time=0,need=0;
while(m--){
topic a;
cin>>a.tid>>a.num>>a.t0>>a.t>>a.dt;
a.q_num=a.finished=0;
topics.push_back(a);
}
cin>>n;
int z=0;
while(n--){
z++;
member a;
int x;
a.work_left=a.last=0;
cin>>a.pid>>a.k;
a.pid=z;
for(int i=0;i<a.k;i++){
cin>>x;
a.pidk.push_back(x);
}
staff.push_back(a);
}
while(1){
for(int i=0;i<topics.size();i++)
if(time>=topics[i].t0&&topics[i].finished+topics[i].q_num<topics[i].num){
topics[i].t0+=topics[i].dt;
topics[i].q_num=min(++topics[i].q_num,topics[i].num-topics[i].finished);
}
sort(staff.begin(),staff.end());
for(int i=0;i<staff.size();i++){
if(!staff[i].work_left)
for(int j=0;j<staff[i].pidk.size();j++)
for(int k=0;k<topics.size();k++)
if(staff[i].pidk[j]==topics[k].tid&&topics[k].q_num){
staff[i].last=time;
staff[i].work_left=topics[k].t;
topics[k].q_num--;
need=max(need,topics[k].t);
if(++topics[k].finished==topics[k].num){
topics.erase(topics.begin()+k);
if(!topics.size()){
time+=need;
goto END;
}
}
goto NEXT_P;
}
NEXT_P:
continue;
}
time++;
need=max(--need,0);
for(int i=0;i<staff.size();i++)
staff[i].work_left=max(--staff[i].work_left,0);
}
END:
kase++;
cout<<"Scenario "<<kase<<": All requests are serviced within "<<time<<" minutes."<<endl;
staff.clear();
topics.clear();
}
return 0;
}

本文迁移自我的 CSDN博客 ,格式可能有所偏差。