`
linest
  • 浏览: 149986 次
  • 性别: Icon_minigender_1
  • 来自: 内蒙古
社区版块
存档分类
最新评论

pat-1017* Queueing at Bank

    博客分类:
  • pat
 
阅读更多
银行8点至17点开 有固定窗口数

来早了要等,没窗口要等,17点后才来就无视

求平均等待时间, 被无视的不统计

注意不是17点一定关门,只要是17点前来的都要服务,即使可能超时

下面代码完全模拟秒数,先排序顾客,滤掉17点后来的
对于每一秒,检查窗口的情况
总体思路是以时间为中心
#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<stdio.h>
#include<algorithm>
#include<queue>



struct Customer
{
	int arrive;
	int cost;
};

int time2int(string s)   
{   
    int time = 0;   
    int loc;   
    int base = 3600;   
    string tmp = s;   
    for(int i=1;i<=3;i++)   
    {   
        loc = tmp.find(":");   
        time += atoi(tmp.substr(0,loc).c_str())*base;   
        tmp = tmp.substr(loc+1);   
        base /= 60;   
    }   
  
    return time;   
}   
  
string int2time(int time)   
{   
    string s;   
    char part[3];   
       
    for(int i=1;i<=3;i++)   
    {   
        sprintf(part,"%02d",time%60);   
        s = string(part) + s;   
        time /= 60;   
        if(i!=3)   
            s = ":"+s;   
    }   
    return s;   
}   

bool compare(Customer a,Customer b)
{
	return a.arrive < b.arrive;
}

// care people come too early  they must wait
int main()
{
	int N;//customer
	int K;//window
	vector<Customer> wait;
	vector<Customer> win;
	vector<Customer> tmp;
	string time;
	int need;

	int total = 0;
	int available;
	int allcus = 0;

	cin>>N;
	cin>>K;
	available = K;
	

	while(N--)
	{
		Customer c;
		cin>>time;
		c.arrive = time2int(time);
		cin>>need;
		c.cost = need * 60;
		if(c.arrive <= 17*3600)  //eliminate customer coming too late
		{
			wait.push_back(c);
			allcus ++;
		}
	}
	sort(wait.begin(),wait.end(),compare);
	
	//serving time
	for(int t = 8*3600; !wait.empty() ;t++)
	{
		//may off at same time
		for(int i = 0;i<win.size();i++)
		{
			win[i].cost --;
			if(win[i].cost == 0)
				available++;   //off window
			else
				tmp.push_back(win[i]);
		}
		win = tmp;
		tmp.clear();
		//on window
		while(available > 0 && wait.size() > 0 && t >= wait[0].arrive)
		{
				win.push_back(wait[0]);
				available --;
				total += t - wait[0].arrive;   //wait time for served customer
				wait.erase(wait.begin());
		}

	}

	if(allcus == 0)
		printf("%.1f\n",0.0);
	else
	{
		double avg = total/(60.0*allcus);
		printf("%.1f\n",avg);
	}

}




网上看到下面代码,效率要高
采用优先队列,自动维护顺序,每次取最小
总体思路以顾客为中心
每次取出最近一个顾客,最近一个窗口

如果窗口时间大于顾客时间,则意味着顾客要等
如果顾客时间大于窗口时间,说明窗口空闲,用户来了即可,不用等,此时把用户的时间赋给窗口,相当于跳过了空闲时间

#include<iostream>
#include<iomanip>
#include<queue>
using namespace std;

struct window
{
   int mm;
   int hh;
   int ss;
   bool operator<(const window& a)const
   {
       if(hh>a.hh)
           return true;
      else if(hh==a.hh&&mm>a.mm)
          return true;
     else if(hh==a.hh&&mm==a.mm&&ss>a.ss)
         return true;
     else
         return false;
    }
};

struct customer
{
    int h;
    int m;
    int s;
    int last;
 bool operator<(const customer& a)const
 {
     if(h>a.h)
       return true;
     else if(h==a.h&&m>a.m)
       return true;
     else if(h==a.h&&m==a.m&&s>a.s)
       return true;
     else
       return false;
  }
};

priority_queue<window> bank;
priority_queue<customer> cu;

int main()
{ 
    int n,k,i;
    cin>>n>>k;
    window w;
    for(i=0;i<k;i++)
    {
        w.ss=0;
        w.mm=0;
        w.hh=8;
        bank.push(w);
     }
    customer cust;
    for(i=0;i<n;i++)
    {
        cin>>cust.h;
        getchar();
        cin>>cust.m;
        getchar();
        cin>>cust.s>>cust.last;
       cu.push(cust);
    }
    int c=0;
    double total=0;
    while(!cu.empty())
    {
         cust=cu.top();
         cu.pop();
         if(cust.h>17||(cust.h==17&&cust.m)||(cust.h==17&&!cust.m&&cust.s))
             break;
         c++;
        w=bank.top();
        bank.pop();
       if(cust.h<w.hh||(cust.h==w.hh&&cust.m<w.mm)||(cust.h==w.hh&&cust.m==w.mm&&cust.s<w.ss))
       {
            total+=(w.hh-cust.h)*60.0+(w.mm-cust.m)+(w.ss-cust.s)/60.0;
            w.mm+=cust.last;
            w.hh+=w.mm/60;
            w.mm%=60;
      }
      else  //have window but the customer not come yet  so no need to wait
      {
            w.ss=cust.s;
            w.mm=cust.m+cust.last;
			w.hh=cust.h+w.mm/60;
            w.mm%=60;
       }
      bank.push(w);
   }
 cout<<fixed<<setprecision(1)<<total/c<<endl;
 return 0;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics