1095 Cars on Campus simulation (30 minutes) car stop


Solution:

Main idea of the title:
Give a record of n cars, and record the license plate number, arrival or departure time of each car. Then give k queries, each query gives a time, find out how many cars parked in the campus at this time point. And get the license plate number and the longest time of the vehicle with the longest total residence time in all vehicles. Each in must match the last time continuous out, and the unmatched ones are not counted (for example, two in a row, the first is not counted, or two out in a row, the second is not counted).

The code is as follows:

//simulation
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<map>
#include<string.h>
#define early 0
#define late 23*3600+59*60+59
using namespace std;

int n,k;//n records, k queries

struct car{
    string name,flag;
    int time;
};

vector<car> vec;
map<string,int> mp_time;
map<string,int> mp;

bool cmp(car a,car b){
    return a.time<b.time;
}

int main(){
    //cin>>n>>k;
    scanf("%d%d",&n,&k);
    vec.resize(n);
    int h,m,s;//Hours, minutes, seconds
    char name_ch[10],flag[10];
    for(int i=0;i<n;i++){
        scanf("%s",name_ch);
        vec[i].name=name_ch;
        scanf("%d:%d:%d",&h,&m,&s);
        scanf("%s",flag);
        vec[i].flag=flag;
        vec[i].time=h*3600+m*60+s;
    }

    sort(vec.begin(),vec.end(),cmp);


    for(int i=0;i<k;i++){
        int temp;
        scanf("%d:%d:%d",&h,&m,&s);
        temp=h*3600+m*60+s;
        int ans=0;
        for(int j=0;j<vec.size();j++){
            if(vec[j].flag=="in"){
                mp[vec[j].name]=vec[j].time;
            }
            if(mp[vec[j].name]!=0&&vec[j].flag=="out"&&vec[j].time>mp[vec[j].name]){
                if(vec[j].time>temp&&mp[vec[j].name]<=temp){
                    ans++;
                }
                if(i==0){
                    mp_time[vec[j].name]=mp_time[vec[j].name]+vec[j].time-mp[vec[j].name];
                }
                mp[vec[j].name]=0;
            }
        }
        printf("%d\n",ans);
    }
    int maxl=-1;
    vector<string> name;
    map<string,int>::iterator it;
    for(it=mp_time.begin();it!=mp_time.end();it++){
        if(it->second>maxl){
            maxl=it->second;
        }
    }
    for(it=mp_time.begin();it!=mp_time.end();it++){
        if(it->second==maxl){
            name.push_back(it->first);
        }
    }
    for(int j=0;j<name.size();j++){
        cout<<name[j]<<" ";
    }
    h=maxl/3600;
    m=(maxl-h*3600)/60;
    s=maxl-h*3600-m*60;
    printf("%02d:%02d:%02d",h,m,s);
    return 0;
}

Posted by bungychicago on Mon, 07 Oct 2019 11:23:43 -0700