Poj Solution 2424

http://poj.org/problem?id=2424

#include"queue"
#include"stdio.h"
using namespace std;

struct node
{
    int time;
    int dinners;
};

int a[3],ans;
queue<node> have[3];


bool init()
{
    char c,t;
    int u[3],t1,t2,i;
    node nd,nd1;

    //cin>>a[0]>>a[1]>>a[2];
    scanf("%d%d%d",&a[0],&a[1],&a[2]);

    if(a[0]==0&&a[1]==0&&a[2]==0)
        return 0;
    
    for(i=0;i<3;i++)
    {
        while(!have[i].empty())
            have[i].pop();
    }
    
    ans=0;
    u[0]=0,u[1]=0,u[2]=0;

    while(1)
    {
        
        do{
            scanf("%c",&c);
        }while(! (c=='#'||(c<='9'&&c>='0')) );
    
        if(c=='#')break;
    
        scanf("%d:%d %d",&t1,&t2,&nd.dinners);

        nd.time=((c-'0')*10+t1)*60+t2;
        i=(nd.dinners-1)/2;
        
    //    if(i>=3||i<0)continue;


        while(!have[i].empty())
        {
            nd1=have[i].front();
                
            if(nd1.time>nd.time)
                break;
            
            ans+=nd1.dinners;
            have[i].pop();
            u[i]--;
        }

        if(u[i]==a[i]&&!have[i].empty())
        {
            nd1=have[i].front();
            if(nd1.time<=nd.time+30)
            {
                ans+=nd1.dinners;
                nd.time=nd1.time;

                have[i].pop();
                u[i]--;
            }
        }

        if(u[i]<a[i])
        {
            u[i]++;
            nd.time+=30;
            if(nd.time<=23*60)
                have[i].push(nd);
        }
    }
    
    for(i=0;i<3;i++)
    while(!have[i].empty())
    {
        nd1=have[i].front();
        ans+=nd1.dinners;
        have[i].pop();
        u[i]--;
    }
    
    return 1;

}    

int main()
{
    while(init())
    {
        printf("%dn",ans);
    }
    return 0;
}


											
This entry was posted in poj. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *