PAT A-1095 Cars on Campus (30 points)

Topic: 1095 Cars on Campus (30 points)
Analysis : It is required to output the number of people in the parking lot at a certain moment. If in and out do not match, this record is invalid. If in is still in after in, the car is invalid. The key to preventing timeout in this question is that the given query is incremental, so you don't need to start traversing from 0 every time.
#include <iostream>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<queue>
#include<math.h>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
#define MAX 999999999
typedef long long ll;
int n,m,k;
struct Node{
    
    
    string plate;
    int time;
    string status;
    int flag;
    int yes;
}node[100001];
map<string,int>ma;
int cmp(Node x, Node y)
{
    
    
    if(x.plate == y.plate)
        return x.time < y.time;
    return x.plate < y.plate;
}
int cmp1(Node x, Node y)
{
    
    
    return x.time < y.time;
}
int main()
{
    
    
    cin>>n>>k;
    for(int i = 0; i <n; i++)
    {
    
    
        string x,y;
        int a,b,c;
        cin>>x;
        scanf("%d:%d:%d",&a,&b,&c);
        cin>>y;
        node[i].plate = x;
        node[i].status = y;
        node[i].time = a * 3600 + b * 60 + c;
        node[i].flag = y[0]=='i'?0:1;
    }
    sort(node,node+n,cmp);
    int mmax = -1;
    for(int i = 0 ; i < n - 1 ;i ++)
    {
    
    
        if(node[i].plate == node[i+1].plate && node[i].flag == 0 && node[i+1].flag == 1){
    
    
            ma[node[i].plate] += node[i+1].time - node[i].time;
            mmax = max(mmax, ma[node[i].plate]);
            node[i].yes = node[i+1].yes = 1;
        }
    }
    sort(node,node+n,cmp1);
    int j = 0,cnt = 0;
    for(int i = 0 ; i < k ; i++)
    {
    
    
        int a,b,c;
        scanf("%d:%d:%d",&a,&b,&c);
        int t = a * 3600 + b * 60 + c;
        for(; j < n && node[j].time <= t;j ++)
        {
    
    
            if(node[j].yes == 1 && node[j].status == "in"){
    
    
                cnt++;
            }
            if(node[j].yes == 1 && node[j].status == "out"){
    
    
                cnt--;
            }
        }
        cout<<cnt<<endl;

    }
    for(auto it = ma.begin();it!=ma.end();it++)
    {
    
    
        if(it->second == mmax)
            cout<<it->first<<" ";
    }
    printf("%02d:%02d:%02d",mmax/3600,mmax%3600/60,mmax%60);
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43567222/article/details/113818845