您的位置:首页 > 其它

HDU 4883 TIANKENG’s restaurant(排序或优先队列模拟)——BestCoder Round #2

2015-08-16 18:46 483 查看


TIANKENG’s restaurant

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Problem Description

TIANKENG manages a restaurant after graduating from ZCMU, and tens of thousands of customers come to have meal because of its delicious dishes. Today n groups of customers come to enjoy their meal, and there are Xi persons in the ith group in sum. Assuming
that each customer can own only one chair. Now we know the arriving time STi and departure time EDi of each group. Could you help TIANKENG calculate the minimum chairs he needs to prepare so that every customer can take a seat when arriving the restaurant?

 

Input

The first line contains a positive integer T(T<=100), standing for T test cases in all.

Each cases has a positive integer n(1<=n<=10000), which means n groups of customer. Then following n lines, each line there is a positive integer Xi(1<=Xi<=100), referring to the sum of the number of the ith group people, and the arriving time STi and departure
time Edi(the time format is hh:mm, 0<=hh<24, 0<=mm<60), Given that the arriving time must be earlier than the departure time.

Pay attention that when a group of people arrive at the restaurant as soon as a group of people leaves from the restaurant, then the arriving group can be arranged to take their seats if the seats are enough.

 

Output

For each test case, output the minimum number of chair that TIANKENG needs to prepare.

 

Sample Input

2
2
6 08:00 09:00
5 08:59 09:59
2
6 08:00 09:00
5 09:00 10:00

 

Sample Output

11
6

 

Source

BestCoder Round #2

 
/****************************************************/

出题人的解题思路:
按时间从早到晚排序后,遇到到达加上人数,遇到离开减去人数,遍历求出最大值即可。
题意:有n组顾客,给你每组顾客的人数、光顾时间和离开时间,因为每位顾客需要一把椅子,问你至少需要多少把椅子才能使每位顾客都有座位

因为顾客不断地更替,后来的可以坐在之前来过走了的顾客的位置,所以有了至少准备多少把椅子之说。

该题有两种方法,一种是出题人提到的方法,将同一组人的到达时间和离开时间分成两部分存入结构体数组,到达时间人数记为正,离开时间人数记为负,每次比较取大值,这样最终就是我们要求的结果

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 10005;
const int inf = 1000000000;
const int mod = 258280327;
struct group
{
int t,x;
}w[2*N];
bool cmp(group x,group y)
{
if(x.t!=y.t)
return x.t<y.t;
return x.x<y.x;
}
int main()
{
int t,n,i,j,h1,m1,h2,m2,ans,k;
scanf("%d",&t);
while(t--)
{
ans=k=0;
scanf("%d",&n);
for(i=j=0;i<n;i++)
{
scanf("%d%d:%d%d:%d",&w[j].x,&h1,&m1,&h2,&m2);
w[j++].t=h1*60+m1;
w[j].x=-w[j-1].x;
w[j++].t=h2*60+m2;
}
sort(w,w+j,cmp);
for(i=0;i<j;i++)
{
k+=w[i].x;
ans=max(ans,k);
}
printf("%d\n",ans);
}
return 0;
}
而另一种方法则是用优先队列模拟顾客关顾与离开的情况,不过要修改一下优先级,具体修改优先级的方法可见链接
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 10005;
const int inf = 1000000000;
const int mod = 258280327;
struct group
{
int x,s,e;
bool operator < (const group &a) const
{
return e>a.e;//最小值优先
}
}w
;
bool cmp(group x,group y)
{
return x.s<y.s;
}
int main()
{
int t,n,i,h1,m1,h2,m2,ans,k;
scanf("%d",&t);
while(t--)
{
ans=k=0;
priority_queue<group> q;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d:%d%d:%d",&w[i].x,&h1,&m1,&h2,&m2);
w[i].s=h1*60+m1;
w[i].e=h2*60+m2;
}
sort(w,w+n,cmp);
for(i=0;i<n;i++)
{
while(!q.empty()&&w[i].s>=q.top().e)
{
k+=q.top().x;
q.pop();
}
k-=w[i].x;
if(k<0)
ans-=k,k=0;
q.push(w[i]);
}
printf("%d\n",ans);
}
return 0;
}
菜鸟成长记
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: