PAT·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, the number of cars parking on campus, and at the end of the day find the cars that have parked for the longest time period.

Input Specification:

Each input file contains one test case. Each case starts with two positive integers N (<= 10000), the number of records, and K (<= 80000) the number of queries. Then N lines follow, each gives a record in the format

plate_number hh:mm:ss status

where plate_number is a string of 7 English capital letters or 1-digit numbers; hh:mm:ss represents the time point in a day by hour:minute:second, with the earliest time being 00:00:00 and the latest 23:59:59; and status is either in or out.

Note that all times will be within a single day. Each “in” record is paired with the chronologically next record for the same car provided it is an “out” record. Any “in” records that are not paired with an “out” record are ignored, as are “out” records not paired with an “in” record. It is guaranteed that at least one car is well paired in the input, and no car is both “in” and “out” at the same moment. Times are recorded using a 24-hour clock.

Then K lines of queries follow, each gives a time point in the format hh:mm:ss. Note: the queries are given in ascending order of the times.

Output Specification:

For each query, output in a line the total number of cars parking on campus. The last line of output is supposed to give the plate number of the car that has parked for the longest time period, and the corresponding time length. If such a car is not unique, then output all of their plate numbers in a line in alphabetical order, separated by a space.

Sample Input:
16 7
JH007BD 18:00:01 in
ZD00001 11:30:08 out
DB8888A 13:00:00 out
ZA3Q625 23:59:50 out
ZA133CH 10:23:00 in
ZD00001 04:09:59 in
JH007BD 05:09:59 in
ZA3Q625 11:42:01 out
JH007BD 05:10:33 in
ZA3Q625 06:30:50 in
JH007BD 12:23:42 out
ZA3Q625 23:55:00 in
JH007BD 12:24:23 out
ZA133CH 17:11:22 out
JH007BD 18:07:01 out
DB8888A 06:30:50 in
05:10:00
06:30:50
11:00:00
12:23:42
14:00:00
18:00:00
23:59:00
Sample Output:
1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09
纯模拟
先按照名字排序 相同名字按照相同时间排序 这样做的好处是能够方便我们逻辑清晰地查找
前后匹配的进出对 这里的录取原则就是out时间之前最近的一个in与他配对(话说我一直没看到题目中说到底是哪一个跟他配对 不知道有没有人从题目中看出来?有的话不妨解释下 )
还有就是计算最长时间的时候 需要把每个车位的所有时间都累积下来 都算作一个参与比较的计算时间单位
然后再输入各时间点 把之前我们处理好的数据一个个扫 扫到超过查询时间就跳出 由于查询时间递增 下一个时间接着从这个位置扫就好 每一个查询时间输出跳出后的记录数量

#include<bits/stdc++.h>
using namespace std;
struct node{
    char id[10];
    int time,io;
}N[10003];
bool cmp(node a,node b){
    return strcmp(a.id,b.id)<0||(strcmp(a.id,b.id)==0&&a.time<b.time);
}
bool cmp2(node a,node b){
    return a.time<b.time;
}
bool cmp3(node a,node b){
    return a.time>b.time||(a.time==b.time&&strcmp(a.id,b.id)<0);
}
int main()
{
    int h,m,s,n,c;
    scanf("%d%d",&n,&c);
    for(int i=1;i<=n;i++){
        char a[3];
        scanf("%s%d:%d:%d%s",N[i].id,&h,&m,&s,a);
        if(a[0]=='i')N[i].io=1;
        else N[i].io=2;
        N[i].time = h*3600+m*60+s;
    }
    sort(N+1,N+1+n,cmp);//按照先按照名字 再按照时间顺序排序
    map<string,int>ct;// 记录每个车号的存在时间
    vector<node>record;//记录有效记录
    vector<node>ans;
    int lngest=-1;
    node p;
    for(int i=1;i<=n;i++){
        if(strcmp(N[i].id,N[i+1].id)==0&&N[i].io==1&&N[i+1].io==2){
            record.push_back(N[i]);
            record.push_back(N[i+1]);
            ct[N[i].id]+=N[i+1].time-N[i].time;
            if(ct[N[i].id]>lngest){
                lngest = ct[N[i].id];
                strcpy(p.id,N[i].id);
                p.time = lngest;
                ans.push_back(p);
            }
            else if(ct[N[i].id]==lngest){
                strcpy(p.id,N[i].id);
                p.time = lngest;
                ans.push_back(p);
            }
        }
    }
    int now=0,j=0;
    sort(record.begin(),record.end(),cmp2);
    for(int i=1;i<=c;i++){
        scanf("%d:%d:%d",&h,&m,&s);
        int cal = h*3600+m*60+s;
        for(;j<record.size();j++){
            if(record[j].io==1){
                if(record[j].time<=cal)now++;
                else break;
            }
            else {//如果这个点是出点
                if(record[j].time<=cal)now--;
                else if(record[j].time>cal)break;
            }
        }
        printf("%d\n",now);
    }
    int save=lngest;
    sort(ans.begin(),ans.end(),cmp3);
    for(int i=0;i<ans.size();i++){
        if(i==0)printf("%s",ans[i].id);
        if(i>0&&save==ans[i].time)printf(" %s",ans[i].id);
    }
    printf(" %02d:%02d:%02d\n",save/3600,save%3600/60,save%60);
    return 0;
}

还写了一个map狂怼的写法 不过第四个测试点一直答案错误
不知道是什么坑。。。

#include<bits/stdc++.h>
using namespace std;
struct node{
    char id[10];
    int time,f;//f=1:in f=2:out 3:query; 4:cut
}N[90005];
struct ans{
    char id[10];
    int time;
};
bool cmp(node a,node b){
    return a.time<b.time||(a.time==b.time&&a.f<b.f);//注意这种扫描的方法排序的话 需要把我们输入的时间排在后面
}
bool cmp2(ans a,ans b){
    return a.time>b.time||(a.time==b.time&&strcmp(a.id,b.id)<0);
}

int main()
{
    char a[3];
    int th,tm,ts,n,p;
    scanf("%d%d",&n,&p);
    for(int i=1;i<=n;i++){
        scanf("%s%d:%d:%d%s",N[i].id,&th,&tm,&ts,a);
        N[i].time = th*3600+tm*60+ts;
        if(a[0]=='i')N[i].f=1;
        else N[i].f=2;
    }
    sort(N+1,N+1+n,cmp);
    map<string,int>timecnt;//记录车位的时间积累
    map<string,int>lastt;//标记上次的位置
    map<string,int>M;//标记状态
    map<string,int>timecal;//标记开始时间
    vector<ans>an;//存储时间最长的答案数组
    int now=0,lngest=-1;
    // 实现计算每个车的运行时间的部分
    for(int i=1;i<=n;i++){
        if(M[N[i].id]==0){//计算操作时间
            if(N[i].f==1){
                M[N[i].id]=1;
                timecal[N[i].id] = N[i].time;//save time
                N[i].f=4;
                lastt[N[i].id] = i;
            }
            else N[i].f=4;// if f=2 没有借就还 错误记录 扔掉
        }
        else if(M[N[i].id]==1){// 配对后面的
            if(N[i].f==1)timecal[N[i].id]=N[i].time,lastt[N[i].id] = i;// 有借再借 保留
            else{
                M[N[i].id]=0;// calculate time 非借车位状态了
                int cal = N[i].time-timecal[N[i].id];
                N[lastt[N[i].id]].f=1;
                timecnt[N[i].id] +=cal;
                if(timecnt[N[i].id]>=lngest){
                    ans p;
                    strcpy(p.id,N[i].id);//***
                    p.time = timecnt[N[i].id];
                    an.push_back(p);
                    lngest = timecnt[N[i].id];
                }
            }
        }
    }

    for(int i=n+1;i<n+1+p;i++){
        scanf("%d:%d:%d",&th,&tm,&ts);
        N[i].time = th*3600+tm*60+ts;
        N[i].f=3;
    }
    sort(N+1,N+1+n+p,cmp);
    now=0;
    for(int i=1;i<n+1+p;i++){
        if(N[i].f==4)continue;
        else if(N[i].f==1)now++;
        else if(N[i].f==2)now--;
        else printf("%d\n",max(now,0));
    }
    sort(an.begin(),an.end(),cmp2);
    int save=0;
    for(int i=0;i<an.size();i++){
        if(i==0)save = an[i].time,printf("%s",an[i].id);
        else if(an[i].time==save)printf(" %s",an[i].id);
    }
    printf(" %02d:%02d:%02d\n",save/3600,((save%3600)/60),((save)%60));
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33859479/article/details/79558443