UVA 822 Queue and A
题目链接:https://vjudge.net/problem/UVA-822
翻译摘自:《算法禁赛入门经典》
题目大意
你的任务是模拟一个客户中心运作情况。客服请求一共有n(1 ≤ n ≤ 20)种主题,每种主 题用 5 个整数描述:tid, num, t0, t, dt,其中 tid 为主题的唯一标识符,num为该主题的请求个 数,t0 为第一个请求的时刻,t 为处理一个请求的时间,dt 为相邻两个请求之间的间隔(为了简化情况,假定同一个主题的请求按照相同的间隔到达)。
客户中心有m(1 ≤ m ≤ 5)个客服,每个客服用至少 3 个整数描述:pid, k, tid 1 , tid 2 , …, tid k,表示一个标识符为 pid 的人可以处理 k 种主题的请求,按照优先级从大到小依次为 tid 1 , tid 2 , …, tid k。当一个人有空时,他会按照优先级顺序找到第一个可以处理的请求。如果有多 个人同时选中了某个请求,上次开始处理请求的时间早的人优先;如果有并列,再输入列表前面的优先。输出最后一个请求处理完毕的时刻。
分析
这道题我是崩溃的,首先 udebug 上的程序跑出来的数据是错误的,我在网上找了一份 AC 代码,和《算法禁赛入门经典》的 AC 代码对拍,两个跑出来还是不一样。
题目是好题,但 不推荐做,因为数据实在太马虎了。
代码如下
1 #include <bits/stdc++.h>
2 using namespace std;
3
4 #define INIT() ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
5 #define Rep(i,n) for (int i = 0; i < (int)(n); ++i)
6 #define For(i,s,t) for (int i = (int)(s); i <= (int)(t); ++i)
7 #define rFor(i,t,s) for (int i = (int)(t); i >= (int)(s); --i)
8 #define ForLL(i, s, t) for (LL i = LL(s); i <= LL(t); ++i)
9 #define rForLL(i, t, s) for (LL i = LL(t); i >= LL(s); --i)
10 #define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
11 #define rforeach(i,c) for (__typeof(c.rbegin()) i = c.rbegin(); i != c.rend(); ++i)
12
13 #define pr(x) cout << #x << " = " << x << " "
14 #define prln(x) cout << #x << " = " << x << endl
15
16 #define LOWBIT(x) ((x)&(-x))
17
18 #define ALL(x) x.begin(),x.end()
19 #define INS(x) inserter(x,x.begin())
20 #define UNIQUE(x) x.erase(unique(x.begin(), x.end()), x.end())
21 #define REMOVE(x, c) x.erase(remove(x.begin(), x.end(), c), x.end()); // 删去 x 中所有 c
22 #define TOLOWER(x) transform(x.begin(), x.end(), x.begin(),::tolower);
23 #define TOUPPER(x) transform(x.begin(), x.end(), x.begin(),::toupper);
24
25 #define ms0(a) memset(a,0,sizeof(a))
26 #define msI(a) memset(a,0x3f,sizeof(a))
27 #define msM(a) memset(a,-1,sizeof(a))
28
29 #define MP make_pair
30 #define PB push_back
31 #define ft first
32 #define sd second
33
34 template<typename T1, typename T2>
35 istream &operator>>(istream &in, pair<T1, T2> &p) {
36 in >> p.first >> p.second;
37 return in;
38 }
39
40 template<typename T>
41 istream &operator>>(istream &in, vector<T> &v) {
42 for (auto &x: v)
43 in >> x;
44 return in;
45 }
46
47 template<typename T>
48 ostream &operator<<(ostream &out, vector<T> &v) {
49 Rep(i, v.size()) out << v[i] << " \n"[i == v.size() - 1];
50 return out;
51 }
52
53 template<typename T1, typename T2>
54 ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
55 out << "[" << p.first << ", " << p.second << "]" << "\n";
56 return out;
57 }
58
59 inline int gc(){
60 static const int BUF = 1e7;
61 static char buf[BUF], *bg = buf + BUF, *ed = bg;
62
63 if(bg == ed) fread(bg = buf, 1, BUF, stdin);
64 return *bg++;
65 }
66
67 inline int ri(){
68 int x = 0, f = 1, c = gc();
69 for(; c<48||c>57; f = c=='-'?-1:f, c=gc());
70 for(; c>47&&c<58; x = x*10 + c - 48, c=gc());
71 return x*f;
72 }
73
74 template<class T>
75 inline string toString(T x) {
76 ostringstream sout;
77 sout << x;
78 return sout.str();
79 }
80
81 inline int toInt(string s) {
82 int v;
83 istringstream sin(s);
84 sin >> v;
85 return v;
86 }
87
88 //min <= aim <= max
89 template<typename T>
90 inline bool BETWEEN(const T aim, const T min, const T max) {
91 return min <= aim && aim <= max;
92 }
93
94 typedef unsigned int uI;
95 typedef long long LL;
96 typedef unsigned long long uLL;
97 typedef vector< int > VI;
98 typedef vector< bool > VB;
99 typedef vector< char > VC;
100 typedef vector< double > VD;
101 typedef vector< string > VS;
102 typedef vector< LL > VL;
103 typedef vector< VI > VVI;
104 typedef vector< VB > VVB;
105 typedef vector< VS > VVS;
106 typedef vector< VL > VVL;
107 typedef vector< VVI > VVVI;
108 typedef vector< VVL > VVVL;
109 typedef pair< int, int > PII;
110 typedef pair< LL, LL > PLL;
111 typedef pair< int, string > PIS;
112 typedef pair< string, int > PSI;
113 typedef pair< string, string > PSS;
114 typedef pair< double, double > PDD;
115 typedef vector< PII > VPII;
116 typedef vector< PLL > VPLL;
117 typedef vector< VPII > VVPII;
118 typedef vector< VPLL > VVPLL;
119 typedef vector< VS > VVS;
120 typedef map< int, int > MII;
121 typedef unordered_map< int, int > uMII;
122 typedef map< LL, LL > MLL;
123 typedef map< string, int > MSI;
124 typedef map< int, string > MIS;
125 typedef multiset< int > mSI;
126 typedef set< int > SI;
127 typedef stack< int > SKI;
128 typedef deque< int > DQI;
129 typedef queue< int > QI;
130 typedef priority_queue< int > PQIMax;
131 typedef priority_queue< int, VI, greater< int > > PQIMin;
132 const double EPS = 1e-8;
133 const LL inf = 0x7fffffff;
134 const LL infLL = 0x7fffffffffffffffLL;
135 const LL mod = 1e9 + 7;
136 const int maxN = 1e5 + 7;
137 const LL ONE = 1;
138 const LL evenBits = 0xaaaaaaaaaaaaaaaa;
139 const LL oddBits = 0x5555555555555555;
140
141 struct Event{
142 int id; // 活动对应实体在数组中的序号
143 int time;
144 bool isRorC; // 是请求到达还是客服空闲事件
145
146 Event(int x, int y, bool z) {
147 time = x;
148 id = y;
149 isRorC = z;
150 }
151
152 bool operator< (const Event &x) const {
153 return time > x.time;
154 }
155 };
156
157 struct ReqInfo {
158 int tid; // 请求id
159 int idx; // 数组下标id
160 int num; // 请求数量
161 int t0; // 第一个请求到达时间
162 int t; // 请求处理耗时
163 int dt; // 两个相邻请求之间的到达间隔
164 };
165
166 istream& operator>> (istream& in, ReqInfo &x) {
167 in >> x.tid >> x.num >> x.t0 >> x.t >> x.dt;
168 return in;
169 }
170
171 struct StaffInfo {
172 int pid; // 职员id
173 int idx; // 数组下标id
174 int k; // 职员所能处理的请求数量
175 VI tids; // 所能处理的请求id,优先级从大到小
176 int last; // 上次开始处理请求的时间
177
178 bool operator< (const StaffInfo &x) const {
179 return last < x.last || last == x.last && idx < x.idx;
180 }
181 };
182
183 istream& operator>> (istream& in, StaffInfo &x) {
184 in >> x.pid >> x.k;
185 x.tids.resize(x.k);
186 Rep(i, x.k) in >> x.tids[i];
187 return in;
188 }
189
190 int N, M, T, ans;
191 vector< ReqInfo > reqs;
192 vector< StaffInfo > staffs;
193
194 priority_queue< Event > waitQ; // 等待队列,按时间升序
195 mSI mInQ; // 当前队列中的所有任务
196 SI freeStaffs; // 空闲员工集合
197
198 bool readInfoAndGenerateEvent() {
199 MII ridToIdx; // 存请求id与实际存的数组下标的对应关系
200
201 cin >> N;
202 if(!N) return false;
203 reqs.resize(N);
204 Rep(i, N) {
205 auto &r = reqs[i];
206
207 cin >> r;
208 ridToIdx[r.tid] = i;
209 r.idx = i; // 把请求id变成数组下标,方便写代码
210
211 Rep(j, r.num) waitQ.push(Event(r.t0 + r.dt * j, i, true)); // 请求到达活动
212 }
213
214 cin >> M;
215 staffs.resize(M);
216 Rep(i, M) {
217 auto &sf = staffs[i];
218
219 cin >> sf;
220 Rep(j, sf.k) sf.tids[j] = ridToIdx[sf.tids[j]];
221 sf.idx = i; // 把客服id变成数组下标,方便写代码
222 sf.last = 0;
223
224 waitQ.push(Event(0, i, false)); // 客服空闲活动
225 }
226
227 return true;
228 }
229
230 struct StaffCompByIdx {
231 bool operator() (int x, int y) const {
232 return staffs[x] < staffs[y];
233 }
234 };
235
236 void solve() {
237 int nowTime = waitQ.top().time;
238 ans = nowTime;
239
240 while(!waitQ.empty() && nowTime == waitQ.top().time) { // 把到达的活动全取出来
241 const Event &e = waitQ.top();
242
243 if(e.isRorC) mInQ.insert(e.id); // 有新请求到达了
244 else freeStaffs.insert(e.id); // 有客服空闲了
245
246 waitQ.pop();
247 }
248
249 // 分配任务
250 while(!mInQ.empty() && !freeStaffs.empty()) { // 任务队列里有请求并且有员工空闲
251 vector< set< int, StaffCompByIdx > > req_staffs(N); // 每种请求可供受理的员工集合
252 bool canAssign = false; // 是否能分配任务
253
254 for(auto &i : freeStaffs) { // 枚举每个空闲客服
255 auto &fs = staffs[i];
256
257 Rep(j, fs.k) { // 依次遍历每一项可处理的请求
258 int tid = fs.tids[j];
259
260 if(mInQ.find(tid) == mInQ.end()) continue;
261 canAssign = true;
262
263 req_staffs[tid].insert(fs.idx); // 第一个可处理的请求
264 break; // 直接退出即可,后面优先级的请求等这轮循环没分配到再进入下一轮分配
265 }
266 }
267
268 if(!canAssign) break; // 没人分配到工作就退出
269
270 Rep(i, N) { // 枚举请求号
271 auto &rs = req_staffs[i];
272
273 while(mInQ.find(i) != mInQ.end() && !rs.empty()) { // 任务队列中有这个请求并且有员工能处理这个请求
274 mInQ.erase(mInQ.find(i));
275
276 int pid = *(rs.begin()); // 排名第一的能处理 i 号请求的员工 id,给他分配任务
277 auto &sf = staffs[pid];
278
279 sf.last = nowTime;
280 waitQ.push(Event(nowTime + reqs[i].t, sf.idx, false)); // 下一个客服空闲请求
281 freeStaffs.erase(sf.idx);
282 rs.erase(pid);
283 }
284 }
285 }
286 }
287
288 int main() {
289 //freopen("MyOutput.txt","w",stdout);
290 //freopen("input.txt","r",stdin);
291 //INIT();
292 while(readInfoAndGenerateEvent()) {
293 ans = 0;
294 freeStaffs.clear();
295
296 while(!waitQ.empty()) solve();
297 printf("Scenario %d: All requests are serviced within %d minutes.\n", ++T, ans);
298 }
299 return 0;
300 }
转载于//www.cnblogs.com/zaq19970105/p/11378887.html
还没有评论,来说两句吧...