PAT (Advanced Level) 1095. Cars on Campus (30)

模拟题。仔细一些即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<cstring>
#include<stack>
#include<vector>
#include<iostream>
using namespace std;

int n,m;
struct Info
{
    string id;
    int time,f;
    int quary;
}tmp[200000+10],s[200000+10];

string ans[200000+10];

struct SJ
{
    string id;
    int time;
}sj[200000+10];

bool cmp(const Info&a,const Info&b)
{
    if(a.id==b.id) return a.time<b.time;
    return a.id<b.id;
}

bool cmp2(const Info&a,const Info&b)
{
    if(a.time==b.time) return a.quary<b.quary;
    return a.time<b.time;
}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        cin>>tmp[i].id;
        int hh,mm,ss; scanf("%d:%d:%d",&hh,&mm,&ss);
        tmp[i].time=hh*60*60+mm*60+ss;
        char op[5]; scanf("%s",op);
        if(op[0]=='i') tmp[i].f=1;
        else tmp[i].f=0;
        tmp[i].quary=0;
    }

    sort(tmp+1,tmp+1+n,cmp);

    int sz=0,p=1;
    while(1)
    {
        if(p>n) break;
        if(tmp[p].f==1)
        {
            if(tmp[p+1].f==0&&tmp[p+1].id==tmp[p].id)
            {
                s[sz++]=tmp[p];
                s[sz++]=tmp[p+1];
                p=p+2;
            }
            else p++;
        }
        else p++;
    }

    int r=0;
    sj[r].id=s[0].id;
    sj[r].time=s[1].time-s[0].time;
    for(int i=2;i<sz;i=i+2)
    {
        if(s[i].id==s[i-1].id)
        {
            sj[r].time=sj[r].time+s[i+1].time-s[i].time;
        }
        else
        {
            r++;
            sj[r].id=s[i].id;
            sj[r].time=s[i+1].time-s[i].time;
        }
    }
    r++;

    int Max=0,u=0;
    for(int i=0;i<r;i++) Max=max(Max,sj[i].time);
    for(int i=0;i<r;i++)
        if(sj[i].time==Max)
            ans[u++]=sj[i].id;
    sort(ans,ans+u);

    for(int i=0;i<m;i++)
    {
        int hh,mm,ss; scanf("%d:%d:%d",&hh,&mm,&ss);
        s[sz].quary=1;
        s[sz++].time=hh*60*60+mm*60+ss;
    }

    sort(s,s+sz,cmp2);

    int num=0;
    for(int i=0;i<sz;i++)
    {
        if(s[i].quary==1)
        {
            printf("%d\n",num);
        }
        else {
            if(s[i].f==1) num++;
            else num--;
        }
    }

    sort(ans,ans+u);
    for(int i=0;i<u;i++)
    {
        cout<<ans[i];
        printf(" ");
    }

    int hh,mm,ss;
    hh=Max/3600; Max=Max-hh*3600;
    mm=Max/60; Max=Max-mm*60;
    ss=Max;

    printf("%02d:%02d:%02d\n",hh,mm,ss);

    return 0;
}

 

posted @ 2016-07-04 21:57  Fighting_Heart  阅读(113)  评论(0编辑  收藏  举报