首页 > ACM题库 > HDU-杭电 > HDU 1103 Flo’s Restaurant-优先队列-[解题报告] C++
2013
11-27

HDU 1103 Flo’s Restaurant-优先队列-[解题报告] C++

Flo’s Restaurant

问题描述 :

Sick and tired of pushing paper in the dreary bleary-eyed world of finance, Flo ditched her desk job and built her own restaurant.

In the small restaurant, there are several two-seat tables, four-seat tables and six-seat tables. A single diner or a group of two diners should be arranged to a two-seat table, a group of three or four diners should be arranged to a four-seat table, and a group of five or six diners should be arranged to a six-seat table.

Flo’s restaurant serves delicious food, and many people like to eat here. Every day when lunch time comes, the restaurant is usually full of diners. If there is no suitable table for a new coming group of diners, they have to wait until some suitable table is free and there isn’t an earlier arrival group waiting for the same kind of tables. Kind Flo will tell them how long they will get their seat, and if it’s longer than half an hour, they will leave for another restaurant.

Now given the list of coming diners in a day, please calculate how many diners take their food in Flo’s restaurant. You may assume it takes half an hour for every diner from taking a seat to leaving the restaurant.

输入:

There are several test cases. The first line of each case contains there positive integers separated by blanks, A, B and C (A, B, C >0, A + B + C <= 100), which are the number of two-seat tables, the number of four-seat tables and the number of six-seat tables respectively. From the second line, there is a list of coming groups of diners, each line of which contains two integers, T and N (0 < N <= 6), representing the arrival time and the number of diners of each group. The arrival time T is denoted by HH:MM, and fixed between 08:00 and 22:00 (the restaurant closes at 23:00). The list is sorted by the arrival time of each group in an ascending order, and you may assume that no groups arrive at the same time. Each test case is ended by a line of “#”.

A test case with A = B = C = 0 ends the input, and should not be processed.

输出:

For each test case, you should output an integer, the total number of diners who take their food in Flo’s restaurant, in a separated line.

样例输入:

1 1 1
10:40 1
10:50 2
11:00 4
#
1 1 1
10:40 1
10:50 2
11:00 2
#
1 2 1
10:30 1
10:40 3
10:50 2
11:00 1
11:20 5
#
0 0 0

样例输出:

7
3
12

http://acm.hdu.edu.cn/showproblem.php?pid=1103

模拟,优先级队列,

注意等待的情况,若值得等待,则被等的人出队,保持时间的连续性

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

int main()
{
    int a,b,c,ta[3],h,min,ti,diners,res;
    int  i;
    priority_queue<int,vector<int>,greater<int> > table[3];
    char s[7];
    while(scanf("%d%d%d",&a,&b,&c)==3&&a)
    {
        ta[0]=ta[1]=ta[2]=res=0;
        while(scanf("%s",s)==1&&s[0]!='#')
        {
            ti=0;
            scanf("%d",&diners);
            for(i=0;s[i]!=':';++i)
                ti=s[i]-'0'+ti*10;
            ti*=60;
            min=0;
            for(++i;s[i];++i)
                min=min*10+s[i]-'0';
            ti+=min;
            if(diners==1||diners==2)
            {
                while(!table[0].empty()&&table[0].top()<=ti)
                {
                    ta[0]--;
                    table[0].pop();
                }
                if(ta[0]<a)
                {
                    ++ta[0];
                    res+=diners;
                    table[0].push(ti+30);
                }
                else if(ti+30<=table[0].top())
                {
                    table[0].push(table[0].top()+30);
                    ++ta[0];
                    res+=diners;
                }
            }
            else if(diners==3||diners==4)
            {
                while(!table[1].empty()&&table[1].top()<=ti)
                {
                    ta[1]--;
                    table[1].pop();
                }
                if(ta[1]<b)
                {
                    ++ta[1];
                    res+=diners;
                    table[1].push(ti+30);
                }
                else if(ti+30<=table[1].top())
                {
                    table[1].push(table[1].top()+30);
                    ++ta[1];
                    res+=diners;
                }
            }
            else
            {
                while(!table[2].empty()&&table[2].top()<=ti)
                {
                    ta[2]--;
                    table[2].pop();
                }
                if(ta[2]<c)
                {
                    ++ta[2];
                    res+=diners;
                    table[2].push(ti+30);
                }
                else if(ti+30<=table[2].top())
                {
                    table[2].push(table[2].top()+30);
                    ++ta[2];
                    res+=diners;
                }
            }
        }
        printf("%d\n",res);
        for(i=0;i<3;++i)
            while(!table[i].empty())
                table[i].pop();
    }

    return 0;
}