1017. Queueing at Bank (25) @ PAT (Advanced Level) Practise

用優先隊列模擬銀行窗口,對等待隊伍用List,並進行一次排序。

但是不知道爲什麼總通不過分值最大的第一個case,後面的13分case倒是通過了。anyway,以後再來啃。

附AC:http://blog.csdn.net/sunbaigui/article/details/8657074


#include<iostream>
#include<iomanip>
#include<queue>
#include<list>
#include<algorithm>

using namespace std;

struct customer
{
	int h;
	int m;
	int s;
	int pt;//process theTime, in minutes
	int wt;//wait theTime, in second
	bool operator < (const customer& t) const
	{
		return (h<t.h?true:((h==t.h && m<t.m)?true:((h==t.h && m==t.m && s<t.s)?true:false)));
	}
};
struct window
{
	int h;
	int m;
	int s;
	bool operator < (const window& t) const
	{
		return (h>t.h?true:((h==t.h && m>t.m)?true:((h==t.h && m==t.m && s>t.s)?true:false)));
	}
};
list<struct customer> wl;//wait line
priority_queue<window> wq;//window priority queue
int TWT=0;//total wait theTime, in second;
int N=0;
int K;

int main()
{
	cin>>N>>K;
	//input customers
	struct customer t;
	for(int i=0;i<N;i++)
	{
		cin>>t.h;getchar();cin>>t.m;getchar();cin>>t.s;cin>>t.pt;
		if(t.h<8)
		{
			t.wt=((8-t.h)*60-t.m)*60-t.s;			
			t.h=8;
			t.m=0;
			t.s=0;
		}
		else
			t.wt=0;
		if(t.h<17|| (t.h==17&&t.m==0&&t.s==0))
			wl.push_back(t);
	}
	wl.sort();
	N=wl.size();
	//initial window priority queue
	struct window w;
	for(int i=0;i<K;i++)
	{
		w.h=8;
		w.m=0;
		w.s=0;
		wq.push(w);
	}
	//do work
	struct window tw;
	struct customer tc;
	while(!wl.empty())
	{
		tw=wq.top();
		wq.pop();
		tc=wl.front();
		if(tc.h>tw.h || (tc.h==tw.h && tc.m>tw.m) || (tc.h==tw.h && tc.m==tw.m && tc.s>=tw.s) )
		{
			TWT+=tc.wt;
			tw.h=tc.h+(tc.m+tc.pt)/60;
			tw.m=(tc.m+tc.pt)%60;
			tw.s=tc.s;
		}
		else
		{
			TWT=TWT+((tw.h-tc.h)*60+tw.m-tc.m)*60+tw.s-tc.s+tc.wt;
			int carry=(tw.m+tc.pt)/60;
			tw.m=(tw.m+tc.pt)%60;
			tw.h+=carry;

		}
		wq.push(tw);
		wl.pop_front();
	}
	if(N!=0)
		cout<<fixed<<setprecision(1)<<(1.0*TWT/60/N)<<endl;
	else
		cout<<"0"<<endl;
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章