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;
}
时间: 2024-08-05 04:02:58

PAT (Advanced Level) 1095. Cars on Campus (30)的相关文章

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

题目信息 1095. Cars on Campus (30) 时间限制220 ms 内存限制65536 kB 代码长度限制16000 B Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the informati

PAT 1095. Cars on Campus (30)

1095. Cars on Campus (30) Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at

PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)

题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车连续多次进入,只取最后一个 2.如果一个车连续多次出去,只取第一个 3.一个车可能出入校园内好几次,停留时间取总和 实际上题目就是让我们求某个时间段内的车辆总和,时间段其实就相当于一个区间,区间求和的话,很快就联想到树状数组和线段树.然而怎么将时间段和区间联系起来呢,那就存储出现在记录和询问里的所有

1095. Cars on Campus (30)

Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, t

PAT (Advanced Level) 1018. Public Bike Management (30)

先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int INF=0x7FFFFFFF; co

PAT (Advanced Level) 1076. Forwards on Weibo (30)

最短路. 每次询问的点当做起点,然后算一下点到其余点的最短路.然后统计一下最短路小于等于L的点有几个. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<algorithm>

PAT (Advanced Level) 1014. Waiting in Line (30)

简单模拟题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> using namespace std; struct X { int st; int len; int en; }p[1500]; queue<int>Q[2

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes