PAT甲级1095 Cars on Campus (30分) 繁琐

1095 Cars on Campus (30分)
Zhejiang University has 8 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 (≤10
​4
​​ ), the number of records, and K (≤8×10
​4
​​ ) 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

易错点:

  • 整理出正确的车牌信息,入 入 的情况,第一次入的记录抛弃
  • 时间是累加的

这题道理都懂,也没用啥高级的数据结构,就是繁琐。。。我最开始偷懒把N个出入信息+K个查询按照时间排个序,然后一个循环输出,结果时间过不了,无奈。想过去只能那这K个查询去N个出入信息哪里比对了

  • 独立N个出入信息
  • 标记处正确的出入信息
  • 读取K个查询
  • 拿这K个查询去正确出入信息哪里查询,得到当时的车辆数目
  • 关于最久的停留时间,遍历出入信息,累加停车时间

就这么个顺序

代码只是留个自己看,太多且杂。。。。。。。。。

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;

struct node
{
    string name;
    int a,b,c;
    int state;
    node():state(3),sum(-1)
    {
        name.resize(7);
    }
    int sum;
};
int cmp(node a,node b)
{
    if(a.a!=b.a)
        return a.a<b.a;
    else if(a.b!=b.b)
        return a.b<b.b;
    else if(a.c!=b.c)
        return a.c<b.c;
    else//这里是针对查询的sate 让查询在同一时间的最后面
        return a.state<b.state;
}

int cmp2(node a,node b)
{
    if(a.sum!=b.sum)
        return a.sum>b.sum;
    else
        return a.name<b.name;
}
int main(void)
{
    int N,K;
    scanf("%d %d",&N,&K);
    node sn[N+1];

    string state;
    for(int i=1; i<=N; i++)
    {

        state.resize(7);
        scanf("%s %d:%d:%d %s",&sn[i].name[0],&sn[i].a,&sn[i].b,&sn[i].c,&state[0]);
        //cout<<sn[i].name<<endl;
        if(state[0]=='i')
            sn[i].state=1;
        else
            sn[i].state=2;
    }
    int sign[N+1]= {0};
    sort(sn+1,sn+N+1,cmp);


    unordered_map<string,int> signMap;
    string name;
    for(int i=1; i<=N; i++)
    {
//        cout<<"jiangui:::"<<signMap["ZD00001"]<<endl;
        name=sn[i].name;
        if(sn[i].state==1)
        {
            if(signMap[name]==0)
            {
                //name这个车牌之前记录完美
                signMap[name]=i;//记录这一次In是那个结点
            }
            else
            {
                //in 遇到in
                //上一次记录不对
                sign[signMap[name]]=0;
                signMap[name]=i;
            }
        }
        else
        {
            if(signMap[name]==0)
            {
                //没有iN直接就是out了,不对
                sign[i]=0;
            }
            else
            {
                sign[signMap[name]]=1;
                sign[i]=1;
                signMap[name]=0;
            }
        }
    }
    
    node vecget[N];
    int gett=0;

    for(int i=1; i<=N; i++)
    {
        if(sign[i]==0)
            continue;

        //vec.push_back(sn[i]);
        vecget[gett]=sn[i];
        gett++;
    }
    sort(vecget,vecget+gett,cmp);



    int summ[K];
    for(int i=0; i<K; i++)
    {
        int a,b,c;

        scanf("%d:%d:%d",&a,&b,&c);
        summ[i]=a*3600+b*60+c;
    }

    

    //那这K个查询 去记录里面查询

    int num1=0;
    int pos1=0;
    for(int i=0; i<gett; i++)
    {
        //这里就是找到所有比当前记录小的查询
        while(vecget[i].a*3600+vecget[i].b*60+vecget[i].c>summ[pos1]&&pos1!=K){
            cout<<num1<<endl;
            pos1++;
        }
        if(pos1==K)
            break;
        if(vecget[i].state==1)
        {
            num1++;

        }
        else if (vecget[i].state==2)
        {
            num1--;
        }

    }
    while(pos1!=K){
          cout<<num1<<endl;
          pos1++;
    }



    unordered_map<string,int> cache;
    unordered_map<string,int> out;
    int maxx=-1;
    for(int i=0; i<gett; i++)
    {
        if(vecget[i].state==1)
        {
            cache[vecget[i].name]=(vecget[i].a*60+vecget[i].b)*60+vecget[i].c;
            //cout<<vec[i].name<<"in "<<vec[i].a<<' '<<vec[i].b<<' '<<vec[i].c<<" sum"<<cache[vec[i].name]<<endl;
        }
        else if(vecget[i].state==2)
        {

            out[vecget[i].name]+=(vecget[i].a*60+vecget[i].b)*60+vecget[i].c-cache[vecget[i].name];
            //  cout<<vec[i].name<<"out "<<vec[i].a<<' '<<vec[i].b<<' '<<vec[i].c<<" sum"<<out[vec[i].name]<<endl;
            if(maxx<out[vecget[i].name])
                maxx=out[vecget[i].name];
            vecget[i].sum=out[vecget[i].name];
        }
    }
    vector<string> maxVec;
    for(int i=0; i<gett; i++)
    {
        if(vecget[i].sum==maxx)
        {
            maxVec.push_back(vecget[i].name);
        }
    }

    sort(maxVec.begin(),maxVec.end());

    for(int i=0; i<maxVec.size(); i++)
    {

        cout<<maxVec[i]<<' ';

    }
    printf("%02d:%02d:%02d",maxx/3600,(maxx-maxx/3600*60*60-maxx%60)/60,maxx%60);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章