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);
}


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