贪心算法——区间覆盖——TIANKENG’s restaurant

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?
InputThe 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. 
OutputFor 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

快开学了,再写一篇寒假训练博客,寒假订的小目标终究还是没有完成啊(还是克服不了自己的惰性啊),开学一定要抓紧时间学习(把自己寒假没写完的题都写完(在此立誓))。

这道题的大致意思是:有一家餐馆,有客人会在不同的时间段在这里吃饭,来一定的客人就要安排一定数量的椅子,问最少安排的椅子数量是多少?这道题卡了半天先前的写法不对没过,先付上自己错误代码和错误思路,供大家参考

/*这道题最开始我是这样想的,先按开始时间排序,然后遍历每组数据
如果某一饭局的结束时间比下一饭局的开始时间晚就让这两个饭局人数相加
,如果比下一饭局开始时间早就比较两个饭局人数大小,取人数多的饭局作为
安排的椅子数量 ,虽然我这样写样例我是过了,自己编写的测试数据也没问题
但就是wa,后来还是没想明白为啥。如果哪位大佬知道我的思路哪有错欢迎指正*/ 
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
	int p;
	int start;//开始时间 
	int end;//结束时间 
}a[100005];
bool cmp1(node x,node y){
	if(x.start==y.start) return x.end>y.end;
	return x.start<y.start;
} 
int main(){
	int t,n;
	int h1,m1,h2,m2;
	scanf("%d",&t);
	while(t--){
		cin>>n;
		for(int i=0;i<n;i++){
			scanf("%d %d:%d %d:%d",&a[i].p,&h1,&m1,&h2,&m2);
			a[i].start=h1*60+m1;
			a[i].end=h2*60+m2;
		}
		sort(a,a+n,cmp1);
		node m=a[0];
		int ans=a[0].p;
		for(int i=1;i<n;i++){
			if(m.end>a[i].start){
				ans=ans+a[i].p;
				m=a[i];
			}
			else{
				ans=max(ans,a[i].p);
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

然后附上这道题的正确写法发,这道题的正确写法还是我参考上一篇博客写的,上一篇博客也是区间覆盖类问题方法很巧妙,上代码

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int num[10010];
int main(){
	int t,n;
	cin>>t;
	//int maxx=0;
	while(t--){
		memset(num,0,sizeof(num));
		cin>>n;
		int start,end;
		int maxx=0;
		while(n--){
			int h1,m1,h2,m2;
			int a;
			//int maxx=0;
			scanf("%d %d:%d %d:%d",&a,&h1,&m1,&h2,&m2);
			start=h1*60+m1;
			end=h2*60+m2;
			for(int i=start;i<end;i++){
				num[i]=num[i]+a;/*统计每个时刻需要的板凳数量 如果和莫个时刻重复 
				就在加上*/ 
				maxx=max(maxx,num[i]);
			}
		}
		printf("%d\n",maxx);
	}
	return 0;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值