分析:借助STL的min_element实现。每次更新最先被占用的桌子,具体见注释。
#include<iostream> #include<algorithm> using namespace std; int main() { int A,B,C; char s[10]; int a[102],b[102],c[102]; int curtime,count,ans; int *p; //桌子最先空闲时间 while(cin>>A>>B>>C,A+B+C) { memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); ans=0; while(cin>>s && s[0]!='#') { curtime=(s[0]-'0')*10+(s[1]-'0'); curtime=curtime*60+(s[3]-'0')*10+(s[4]-'0'); cin>>count; if(count==1||count==2) { p=min_element(a,a+A); //取得最小值(也即桌子的最早空余时间) if(*p<=curtime+30) //顾客等30分钟后是否有座位 { if(*p<=curtime) *p=curtime+30; //最先被占用的桌子的顾客已经离开了,新顾客可以入座 else *p+=30; //新顾客还需等待 ans+=count; } } if(count==3||count==4) { p=min_element(b,b+B); //取得最小值(也即桌子的最早空余时间) if(*p<=curtime+30) //顾客等30分钟后是否有座位 { if(*p<=curtime) *p=curtime+30; //最先被占用的桌子的顾客已经离开了,新顾客可以入座 else *p+=30; //新顾客还需等待 ans+=count; } } if(count==5||count==6) { p=min_element(c,c+C); //取得最小值(也即桌子的最早空余时间) if(*p<=curtime+30) //顾客等30分钟后是否有座位 { if(*p<=curtime) *p=curtime+30; //最先被占用的桌子的顾客已经离开了,新顾客可以入座 else *p+=30; //新顾客还需等待 ans+=count; } } } cout<<ans<<endl; } return 0; }
HDU ACM 1103 Flo's Restaurant
时间: 2024-08-08 10:34:49