[NOIP模拟]购买板凳

来源:互联网 发布:mt4编程书籍 编辑:程序博客网 时间:2024/04/23 23:59

样例输入1:

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

样例输出1:

11

样例输入2:

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

样例输出2:

6

数据范围:
对于95%的数据:n≤10000;
对于100%的数据:n≤100000;1≤x≤100;0≤a,c<24;0≤b,d<60。
题目分析:
考试总结:这道题本来不难,但是考试的时候,却全挂,我在记录时间的时候,我以为我的if语句能够去重,但是实际上并不完全。考后修改后,也只能对八个点(为了找到剩下两个点错的原因,也耗费了很久的时间),还不如最后一起去重,简洁无错。(其实这道题不用去重的,不过我的写法要)。
分析:你可以把每个时间点所对应的人数增减记录下来(把时间转化成分钟数存数组),最后扫一遍,取各个时间点存在的人数中的最大值。
PS:下附代码是我考试代码修改正确后的版本,与上述分析不同,我是在判断剩余板凳数,其实是我绕远了。
附代码:

#include<iostream>#include<cstdlib>#include<cstdio>#include<ctime>#include<cmath>#include<cstring>#include<string>#include<cctype>#include<iomanip>#include<algorithm>using namespace std;const int N=2e5+100;int n,ans,num,a,b,c,d,sum,x,y;int tot,w[N],bjl[N],bjr[N];int readint()                                                                       {    char ch;int i=0,f=1;    for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());    if(ch=='-') {ch=getchar();f=-1;}    for(;ch>='0'&&ch<='9';ch=getchar()) i=(i<<3)+(i<<1)+ch-'0';    return i*f;}int main(){    //freopen("chair.in","r",stdin);    //freopen("chair.out","w",stdout);    n=readint();    for(int i=1;i<=n;i++)    {        num=readint();        a=readint();b=readint();        c=readint();d=readint();        x=a*60+b;y=c*60+d;        w[++tot]=x;//将时间存入数组        w[++tot]=y;        bjl[x]+=num;bjr[y]+=num;//记录这两个时间点人数的变化        if(num>ans) ans=num;    }    sort(w+1,w+tot+1);    tot=unique(w+1,w+tot+1)-w-1;//去重    sum=ans; num = 0;//sum代表剩余的板凳数    for(int i=1;i<=tot;i++)//这里你可以直接枚举1~1440,也就无需去重了    {        if(bjr[w[i]]!=0) sum+=bjr[w[i]];        if(bjl[w[i]]!=0)        {            if(sum>=bjl[w[i]]) sum-=bjl[w[i]];            else {ans+=bjl[w[i]]-sum;sum=0;}//不够,于是补上差的        }    }    printf("%d",ans);    return 0;}
原创粉丝点击