1014. Waiting in Line (30)
2016-02-29 23:15
561 查看
1014. Waiting in Line (30)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which devides the waiting area into two parts. The rules for the customers to wait in line are:
The space inside the yellow line in front of each window is enough to contain a line with M customers. Hence when all the N lines are full, all the customers after (and including) the (NM+1)st one will have to wait in a line behind the yellow line.
Each customer will choose the shortest line to wait in when crossing the yellow line. If there are two or more lines with the same length, the customer will always choose the window with the smallest number.
Customer[i] will take T[i] minutes to have his/her transaction processed.
The first N customers are assumed to be served at 8:00am.
Now given the processing time of each customer, you are supposed to tell the exact time at which a customer has his/her business done.
For example, suppose that a bank has 2 windows and each window may have 2 custmers waiting inside the yellow line. There are 5 customers waiting with transactions taking 1, 2, 6, 4 and 3 minutes, respectively. At 08:00 in the morning, customer1 is
served at window1 while customer2 is served at window2. Customer3 will wait in front of window1 and
customer4 will wait in front of window2. Customer5 will wait behind the yellow line.
At 08:01, customer1 is done and customer5 enters the line in front of window1 since that line seems shorter now. Customer2 will
leave at 08:02, customer4 at 08:06, customer3 at 08:07, and finally customer5 at 08:10.
Input
Each input file contains one test case. Each case starts with a line containing 4 positive integers: N (<=20, number of windows), M (<=10, the maximum capacity of each line inside the yellow line), K (<=1000, number of customers), and Q (<=1000, number of customer
queries).
The next line contains K positive integers, which are the processing time of the K customers.
The last line contains Q positive integers, which represent the customers who are asking about the time they can have their transactions done. The customers are numbered from 1 to K.
Output
For each of the Q customers, print in one line the time at which his/her transaction is finished, in the format HH:MM where HH is in [08, 17] and MM is in [00, 59]. Note that since the bank is closed everyday after 17:00, for those customers who cannot be served
before 17:00, you must output "Sorry" instead.
Sample Input
2 2 7 5 1 2 6 4 3 534 2 3 4 5 6 7
Sample Output
08:07 08:06 08:10 17:00 Sorry
AC代码1
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std;
int N,M,K,Q;
int findShortLine(vector<queue<int>>);
string printStr(int n);
int main() {
cin>>N>>M>>K>>Q;
vector<int>cusTime(K);
vector<int>soluNums(Q);
for(int i=0; i<K; i++)
cin>>cusTime[i];
queue<int>waiting;
for(int i=0; i<K; i++) //入队.
waiting.push(i);
vector<int>spendTime(K,0);//每人处理完时花费时间。
vector<queue<int>>lines(N,queue<int>());
int startTime=480,endTime=1020;//8点开,17点结束。
while(!waiting.empty()) {
int res = findShortLine(lines);
if(res==-1) { //上班前队列已经排满不再排。
break;
} else {
if(lines[res].empty()) {
spendTime[waiting.front()]=startTime+cusTime[waiting.front()];
} else {
spendTime[waiting.front()]=spendTime[lines[res].back()]+cusTime[waiting.front()];
}
lines[res].push(waiting.front());
waiting.pop();
}
}
for(int cur = startTime+1; cur<=endTime; ++cur) {
if(waiting.empty())
break;
else {
for(int i=0; i<lines.size(); ++i) {
if(!lines[i].empty()) {
if(spendTime[lines[i].front()]==cur) {
spendTime[waiting.front()]=spendTime[lines[i].back()]+cusTime[waiting.front()];
lines[i].pop();
lines[i].push(waiting.front());
waiting.pop();
if(waiting.empty())
break;
}
}
}
}
}
while(!waiting.empty()){
spendTime[waiting.front()]=cusTime[waiting.front()]+1021;
waiting.pop();
}
for(int i=0; i<Q; i++) {
cin>>soluNums[i];
if(spendTime[soluNums[i]-1]-cusTime[soluNums[i]-1]>=1020)
cout<<"Sorry"<<endl;
else
cout<<printStr(spendTime[soluNums[i]-1])<<endl;
}
return 0;
}
int findShortLine(vector<queue<int>> lines) {
int returnMark = -1,shortNum=M;
for(int i=0; i<lines.size(); i++) {
if(lines[i].size()==0) {
returnMark = i;
break;
} else {
if(lines[i].size()<shortNum) {
returnMark = i;
shortNum = lines[i].size();
}
}
}
return returnMark;
}
string printStr(int n) {
char p[6];
int one = n%60,two = n/60;
sprintf(p,"%02d:%02d",two,one);
return p;
}
AC代码2 代码优化后
#include<iostream>
#include<string>
#include<cstdio>
#include<cmath>
#include<queue>
#include<algorithm>
#include<limits.h>
using namespace std;
int N,M,K,Q;
string printStr(int n);
int main() {
cin>>N>>M>>K>>Q;
vector<int>cusTime(K);
for(int i=0; i<K; i++)
cin>>cusTime[i];
vector<int>spendTime(K,0);//每人处理完时花费时间。
vector<queue<int>>lines(N);
int startTime=480;//8点开,17点结束。
vector<int>queue_time(N,startTime);
int queue_id = 0;
int customerId = 0;
for(;customerId<M*N&&customerId<K;customerId++){
lines[queue_id].push(customerId);
spendTime[customerId] = queue_time[queue_id]+cusTime[customerId];
queue_time[queue_id] = spendTime[customerId];
queue_id = (queue_id+1)%N;
}
for(;customerId<K;customerId++){
int min_time = INT_MAX;//double min_time = pow(2,32)-1;
int min_queue_id = -1;
for(int i=0; i<lines.size(); ++i) {
int t = lines[i].front();
if(min_time>spendTime[t]) { //查询最先离开队列的用户。
min_time = spendTime[t];
min_queue_id = i;
}
}
lines[min_queue_id].pop();
lines[min_queue_id].push(customerId);
spendTime[customerId] = queue_time[min_queue_id]+cusTime[customerId];
queue_time[min_queue_id] = spendTime[customerId];
}
int query;
for(int i=0; i<Q; i++) {
cin>>query;
if(spendTime[query-1]-cusTime[query-1]>=1020)
cout<<"Sorry"<<endl;
else
cout<<printStr(spendTime[query-1])<<endl;
}
return 0;
}
string printStr(int n) {
char p[6];
int one = n%60,two = n/60;
sprintf(p,"%02d:%02d",two,one);
return p;
}
相关文章推荐
- 【技术】正则表达式—匹配电话号码,网址链接,Email地址
- AI快捷键
- Context initialization failed
- CodeForces 608 A. Saitama Destroys Hotel(水~)
- 172. Factorial Trailing Zeroes
- 线程本地存储(Windows和Linux) http://blog.csdn.net/wzhwho/article/details/6427596
- Container With Most Water
- wait3和wait4 Functions
- SSH连接时出现Host key verification failed的原因及解决方法
- Low-rank representation with local constraint for graph construction
- git push origin master出错:error: failed to push some refs to
- STL容器与拷贝构造函数 http://blog.csdn.net/heyutao007/article/details/6702626
- Train Problem II(卡特兰数+大数乘除)
- C++ vector 类学习笔记 http://blog.csdn.net/whz_zb/article/details/6827999
- ZOJ Paint the Grid Reloaded
- 【杭电oj】1702 - ACboy needs your help again!(栈和队列)
- HDU 1022 Train Problem I(stack)
- View、Canvas、Paint
- 详解Paint的setShader(Shader shader)
- aix5.3下安装svn命令