该题是一道队列的题目 , 也是一道模拟题, 考虑桌子时间小的,只要有桌子且等待时间在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);
}
}