【PAT甲级-1095】题解

#include<iostream>
#include<algorithm>
#include<cstring>
#include<map> 
using namespace std;
struct record {
    char id[10];
    int hh, mm, ss, time;
    char status[4];
}rec[4000001], valid[4000001];
bool cmp(record a, record b) {
    int x= strcmp(a.id, b.id);
    if(x != 0) return x < 0;
    else return a.time < b.time;
}
bool cmp1(record a, record b) {
    return a.time < b.time;
}
int main() {
    int n, k, hh, mm, ss;
    cin >> n >> k;
    map<string, int> parkTime;
    for(int i = 0; i < n; i++) {
        char st[5];
        scanf("%s %d:%d:%d %s\n", rec[i].id, &rec[i].hh, &rec[i].mm, &rec[i].ss, &rec[i].status);
        rec[i].time = rec[i].hh * 3600 + rec[i].mm * 60 + rec[i].ss;
    }
    sort(rec, rec + n, cmp);
    int num = 0, maxTime = -1;
    for(int i = 0; i < n - 1; i++) {
    	if(!strcmp(rec[i].id, rec[i + 1].id) && !strcmp(rec[i].status, "in") && strcmp(rec[i + 1].status, "in")) {
    		valid[num++] = rec[i];
    		valid[num++] = rec[i + 1];
    		int inTime = rec[i + 1].time - rec[i].time;
    		if(!parkTime.count(rec[i].id)) parkTime[rec[i].id] = 0;
    		parkTime[rec[i].id] += inTime;
            maxTime = max(maxTime, parkTime[rec[i].id]);
		}
	}
    int now = 0, numCar = 0;
    sort(valid, valid + num, cmp1);
    for(int i = 0; i < k; i++) {
        int hh, mm, ss;
        scanf("%d:%d:%d", &hh, &mm, &ss);
        int time = hh * 3600 + mm * 60 + ss;
        while(now < num && valid[now].time <= time) {
            if(!strcmp(valid[now].status, "in")) numCar++;
            else numCar--;
            now++;
        }
        printf("%d\n", numCar);
    }
    map<string, int>::iterator it;
    for(it = parkTime.begin(); it != parkTime.end(); it++) {
        if(it->second == maxTime) {
            printf("%s ", it->first.c_str());
        }
    }
    printf("%02d:%02d:%02d\n", maxTime / 3600, (maxTime % 3600) / 60, maxTime % 60);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值