poj 2424 Flo's Restaurant

该题是一道队列的题目 ,  也是一道模拟题, 考虑桌子时间小的,只要有桌子且等待时间在30以内,  就可入座,tt即可加一。。  借鉴了下大神的思路, 用来优先队列的写法和原始的模拟队列写法。。



原始代码:



#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cstdlib>
using namespace std;
int main()
{
    int t[1000],f[1000],s[1000];
    int a,b,c;
    int sn=60*23;
    while(~scanf("%d%d%d",&a,&b,&c))  //234桌的人数 
    {
        if(a==0&&b==0&c==0)  break;
        int tt=0,time;
        memset(t,0,sizeof(t));
        memset(f,0,sizeof(f));
        memset(s,0,sizeof(s));
        char shi[1000];
        while(1)
        {
            int k; 
            scanf("%s",shi);  //输入来的时间 
            if(shi[0]=='#')  break;
            scanf("%d",&k);  //输入人数 
            int x=(shi[0]-'0')*10+shi[1]-'0';
            int y=(shi[3]-'0')*10+shi[4]-'0';
            time=x*60+y;  //求来的时间 
            if(time>sn)  continue;
            if(k==1||k==2)
            {
                sort(t,t+a);
                if(time+30>=t[0])
                {
                    if(time>=t[0])
                    {
                        t[0]=time+30;
                        tt+=k;
                    }
                    else 
                    {
                        t[0]+=30;
                        tt+=k;
                    }
                }
            }
            else if(k==3||k==4)
            {
                sort(f,f+b);
                if(time+30>=f[0])
                {
                    if(time>=f[0])
                    {
                        f[0]=time+30;
                        tt+=k;
                    }
                    else 
                    {
                        f[0]+=30;
                        tt+=k;
                    }
                }
            }
            else if(k==5||k==6)
            {
                sort(s,s+c);
                if(time+30>=s[0])
                {
                    if(time>=s[0])
                    {
                        s[0]=time+30;
                        tt+=k;
                    }
                    else 
                    {
                        s[0]+=30;
                        tt+=k;
                    }
                }
            }
        }
        printf("%d\n",tt);
    }
    return 0;
} 






优化代码


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<vector>
#include<cstdlib>
#include<functional>
using namespace std;
int a, b, c, tot, t, sum, ans = 0;
char s[20];

int main()
{
	while (scanf("%d%d%d", &a, &b, &c), a + b + c)
	{
		ans = 0;
		priority_queue<int, vector<int>, greater<int> > p[3];
		for (int i = 0; i < a; i++) p[0].push(0);
		for (int i = 0; i < b; i++) p[1].push(0);
		for (int i = 0; i < c; i++) p[2].push(0);
		while (scanf("%s", s), s[0] != '#')
		{
			t = 600 * s[0] + 60 * s[1] + 10 * s[3] + s[4] - '0' * 671;
			scanf("%d", &sum);
			tot = (sum - 1) / 2;  //看来的人个数是多少 
			if (!p[tot].empty())
			{
				int q = p[tot].top();
				if (q <= t + 30)
				{
					q = max(q, t) + 30;
					if (q <= 23 * 60)
					{
						p[tot].pop();
						p[tot].push(q);
						ans += sum;
					}
				}
			}
		}
		printf("%d\n", ans);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值