码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4883 TIANKENG’s restaurant

时间:2015-08-02 18:26:22      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

TIANKENG’s restaurant

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1616    Accepted Submission(s): 580


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
 
 
题意:
 
         这道题是让输入一个数,代表有多少个事例,然后每个事例的第一行输入m代表有m组客人要来,然后输入m行,每一行输入这一组需要的板凳数和起始时间和结束时间,求出这个事例所需要的板凳的个数!
 
思路:
 
       通过求每个时刻所需要的板凳的个数,然后进行比较找最大的就是所需要的最大值!(在求每个时刻所需要的板凳的个数的同时将最大值求出来),具体看代码:
 
第一次的错误思路:
 
//错误的思路!!!!!!(花费两个小时就没改出错误,原来思路错了) 
//1-2:2   1.5-3:3  2-4:4
//用这种思路想的话,就得用9个板凳,但是实际上7个就行了,所以思路有问题@@!1!
//板凳问题是考虑区间重叠问题,而南阳的14会场安排,杭电的2037今年暑假不AC这题还不一样,
//这两道是是求不重叠区间的个数最多的问题,二这个题是求重叠区间的问题,完全不同,以后看见
//不重叠区间的话就要想想会场安排和今年暑假不AC,当看见重叠区间的问题的话,就考虑板凳问题! 
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;

struct node 
{
	int num;
	int t1;
	int t2;
}a[10005];

int cmp(node u,node v)
{
	if(u.t2==v.t2)
		return u.num>v.num;
	return u.t2<v.t2;
}

int main()
{
	int n,m,i,j,k,b,c,d,e,t,s,sum;
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d",&n);
		for(i=0;i<n;i++)
		{
			scanf("%d %d:%d %d:%d",&a[i].num,&b,&c,&d,&e);
			a[i].t1=b*100+c;
			a[i].t2=d*100+e;
		}
		sort(a,a+n,cmp);
		for(i=1,t=a[0].t2,s=a[0].num;i<n;i++)
		{
			if(a[i].t1<t&&a[i].t2>t)
			{
				s+=a[i].num;
				t=a[i].t2;
			}
			else if(a[i].t1<t&&a[i].t2==t) 
			{
				s+=a[i].num;
			}
			else if(a[i].t1>=t&&a[i].num<=s)
			{
				t=a[i].t2;
			}
			else if(a[i].t1>=t&&a[i].num>s)
			{
				s+=(a[i].num-s);
				t=a[i].t2;
			}
		}
		printf("%d\n",s); 
	}
return 0;
}

第二次的超时思路:
 
 
/*
用时:1000ms 
*/
//这种方法超时!思路是为了求所需要的最多的板凳,所以我们要算出都是在什么时候需要板凳,所以
//我算了时间的最大值和最小值,然后,然后我再遍历看看输入的哪个时间段里面有这个时间,然后就
//说明这个时间需要凳子的个数需要加上这么多,还有很多时间段没有包含这个时刻我也必须遍历,所以花费了不少的时间
//并且还有输入数据需要花费时间,给时间排序需要花费时间,所以这么多操作导致了超时的现象,
//以后要学会变相的看待问题,如果求每个时刻的板凳不好一下子求出来的话,我们可以一下子求
//一个时间段的时间对应的板凳,都对应的赋值给他们,再输入第二个时间段各自对应的凳子,再对应的加起来
//然后求最大值,到求最大值的时候我们就又会遇到问题,那就是这个时间数组从哪个值开始,到哪个
//值结束,我们总不能重用循环来遍历找最大值吧,所以我们又需要巧妙的用条件语句穿擦到其中,将
//最大值算出来!每次就多算一条语句,又省去一个循环找最大值的时间,所以就不超时了! 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct node
{
	int num;
	int t1;
	int t2;
}a[10005];
int s[2500];

int cmp(node u,node v)
{
	if(u.t2==v.t2)
		return u.t1<v.t1;
	return u.t2<v.t2;
}

int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int n,m,i,j,k,b,c,d,e,t,mx;
	scanf("%d",&n);
	while(n--)
	{
		memset(s,0,sizeof(s));
		scanf("%d",&m);
		for(i=0;i<m;i++)
		{
			scanf("%d %d:%d %d:%d",&a[i].num,&b,&c,&d,&e);
			a[i].t1=b*60+c;
			a[i].t2=d*60+e;
		}
		sort(a,a+m,cmp);
		
		for(i=a[0].t1,t=0,mx=0;i<=a[m-1].t2;i++)
		{
			for(j=0;j<m;j++) 
			{
				if(i>=a[j].t1&&i<a[j].t2)
				  s[t]+=a[j].num;
				else if(i<a[j].t1)
				   break;
			}
			mx=max(mx,s[t]);
			t++;
		}
		printf("%d\n",mx);
	}
	return 0;
}

经过艰辛的过程之后,终于AC了(方法很好,以后要想着用):
 
//用时:967MS
//这道题的这种方法比较奇葩,他需要统计每个时刻需要多少个板凳,求其对应的板凳的最大值!
//在开始到结束这个时间段内,都需要板凳,所以那些时刻对应的数组都需要加上板凳的数目
//第二次输入的时候,也要进行计算开始的时间和结束的时间,然后再开始和结束的时间内都需要在对应的数组上加上
//所需板凳的数目;以此类推,直到m个为止,这既能算出每个时刻所需要的凳子,又能算出哪个时刻所需要的凳子最多!
//并能算出凳子最多是多少!这种方法真是太好了! 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int main()
{
	int n,m,i,j,k,t_begin,t_end,a,b,c,d,e,t[2000],max;
	scanf("%d",&n);
	while(n--)
	{
		memset(t,0,sizeof(t));
		scanf("%d",&m);
		for(i=0,max=0;i<m;i++)//通过直接嵌套的方法,带输入数值,带计算答案,又减少了m次循环! 
		{
			scanf("%d %d:%d %d:%d",&a,&b,&c,&d,&e);
			t_begin=b*60+c;
			t_end=d*60+e;
			for(j=t_begin;j<t_end;j++)//这个开始的时间与结束的时间的间隔比较短,循环的次数也随之减少 
			{
				t[j]+=a;
				max=(t[j]>max)?t[j]:max;
			}
		}
		printf("%d\n",max);
	}
	return 0;
}

 
 

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 4883 TIANKENG’s restaurant

标签:

原文地址:http://blog.csdn.net/dxx_111/article/details/47208237

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!