F - TIANKENG’s restaurant hd 4886

F - TIANKENG’s restaurant

Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status
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


  1. 题意:给出n组饭客,每组数据包括这一组的人数,去饭店的时间,离开饭店的时间。求饭店最多会有多少人。
  2. 思路:将所有时刻的人数都表示出来,选择最大的一个。
  3. 失误:刚开始用结构体排序,结束时间早的排在前面,查找时间区间覆盖的最大人数,需要两层循环,超时。按时刻找最大人数不优化也会超时,有两种优化方案,如下.
  4. 代码如下:

1.

```
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    int t,i,n,max,time[2222],h1,h2,m1,m2,peo,str,end;
    cin>>t;

    while(t--)
    { 
         memset(time,0,sizeof(time));//将整个数组清0 
         cin.sync_with_stdio(false); //加块读取速度 
         cin>>n;
         for(i=1;i<=n;++i) 
         {
              cin>>peo;
              scanf("%d:%d %d:%d",&h1,&m1,&h2,&m2);//:不可输入成':' 双引号
                                                   //内非格式控制符原样输出 
              str=h1*60+m1;//将一天24小时用分钟表表示出来 
              end=h2*60+m2; //记录i组来的时刻与走的时刻 

              time[str]+=peo; //每个组占据的时间段表示出来 对于区间[str,end),内的所有 
              time[end]-=peo;//该组的人都在 都应为peo个,end时刻时改组的人离开 剩余人数为0 
         }

         max=0;
         for(i=1;i<=1500;++i)//一般思维是将任一时刻出现的人数都表示出来;
         {                   //取最大人数的时刻为最优解 
              time[i]+=time[i-1];//明显时间复杂度较大 可能超时 
              if(time[i]>max)    //此代码进行优化  将内层循环提到外面 
              max=time[i]; //此步骤思想为累加 任一时刻 可分两种情况一种是在end之前  
         }                //    一种之后(include end)        对应上面的代码理解 

         cout<<max<<endl;

     } 
    return 0;
}

2.

 #include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int a[2000],n,h1,h2,m1,m2,sum1,sum2,maxx=0,T,m,i;
    scanf("%d",&T);

    while(T--)
    {

      scanf("%d",&m);
      memset(a,0,sizeof(a));

      while(m--)
     {

        scanf("%d%d:%d%d:%d",&n,&h1,&m1,&h2,&m2);
        sum1=h1*60+m1;
        sum2=h2*60+m2;

        for(i=sum1;i<sum2;i++)//查找策略:从每一组的时间区间查找 
        {                     //对于时间的每一时刻人数只要增加,就与maxx 
           a[i]+=n;           //判断大小,这个明显比第一种时间复杂度大 
           maxx=max(maxx,a[i]); 
        }
     }
    }

    printf("%d\n",maxx);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值