本文主要是介绍1014. Waiting in Line @ PAT (Advanced Level) Practise,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
NOTICE:
1.题目中讲的“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.” 其实是说开始服务时间不能超过17:00而不是服务结束时间。
2.更简洁的代码:http://blog.csdn.net/huajh7/article/details/7918144
3.这样的时间模拟方式是有问题的,在这里没有出现问题是因为所有顾客是同时到达的。在A1017中的类似问题则不能用这种方式。比如一个最小的timeToGo是3分钟,如果期间一个顾客到来,且窗口有空的话,则这个顾客也需要等到3分钟时被服务。一个较好的方式是窗口用优先队列,窗口结构中记录窗口时间。To:A1017
#include<iostream>
#include<deque>
#include<vector>
#include<string>
#include<sstream>using namespace std;int N,M,K,Q;
int timeFinishArray[1004];
int tCount=0;
struct customer{int id;int time;
};
deque<customer> qc;
vector< deque<customer> > vqc;stringstream ss;void initialization();
void dowork();
string convertToTime(int);
void printResult();
bool fillIn();
bool haveCustomer();int main()
{initialization();while(haveCustomer()){dowork();}printResult();return 0;
}
void initialization()
{cin>>N>>M>>K>>Q;for(int i=0;i<N;i++){deque<customer> tmpq;vqc.push_back(tmpq);}for(int i=0;i<K;i++){timeFinishArray[i]=-1;struct customer tmpC;tmpC.id=i;cin>>tmpC.time;qc.push_back(tmpC);}while(fillIn());return;
}
void dowork()
{int minTimeDeque=0;int minTimeToGo=0;for(int i=0;i<N;i++){if(!vqc[i].empty()){if(vqc[i].front().time<minTimeToGo || minTimeToGo==0){minTimeToGo=vqc[i].front().time;}}}if(minTimeToGo==0)return;tCount+=minTimeToGo;for(int i=0;i<N;i++){if(!vqc[i].empty()){vqc[i].front().time-=minTimeToGo;if(vqc[i].front().time==0){timeFinishArray[vqc[i].front().id]=tCount;vqc[i].pop_front();if(tCount>=540)vqc[i].clear();}}}if(tCount<540){while(fillIn());}else{qc.clear();}return;
}
string convertToTime(int i)
{string s;int h,m;ss.clear();ss.str("");// important !h=i/60 + 8;m=i%60;if(h<10)ss<<0;ss<<h<<":";if(m<10)ss<<0;ss<<m;s=ss.str();return s;
}void printResult()
{int c;for(int i=0;i<Q;i++){cin>>c;c--;if(timeFinishArray[c]==-1)cout<<"Sorry"<<endl;elsecout<<convertToTime(timeFinishArray[c])<<endl;}return;
}
bool fillIn()
{if(qc.size()==0)return false;int minDeque=0;for(int i=1;i<N;i++){if(vqc[i].size()<vqc[minDeque].size())minDeque=i;}if(vqc[minDeque].size()<M){vqc[minDeque].push_back(qc.front());qc.pop_front();return true;}elsereturn false;
}bool haveCustomer()
{for(int i=0;i<N;i++){if(!vqc[i].empty()){return true;}}return false;
}
这篇关于1014. Waiting in Line @ PAT (Advanced Level) Practise的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!