PAT甲级1095 【Cars on Campus】 (30)

3 篇文章 0 订阅

分析:注意同一辆车每一个in与out匹配才是有效数据,于是第一次按车牌排序,确定有效记录,第二次按时间排序,然后对应询问的时间进行模拟进出,最后需要注意的一点就是最长停留的车辆还要处理到23:59:59时才能确定。

#include<cstdio>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<set>
using  namespace std;
const int maxn = 1e4+8;

typedef struct Car{
	string cid;
	int time, status, flag; //status 1-on 0-out  flag 1-valid 0-unvalid
}Car;
Car car[maxn];
map<string, int> mp, stay;
set<string> res;

bool cmp2(Car a, Car b){
	return a.flag != b.flag ? a.flag > b.flag : a.time != b.time ? a.time < b.time : a.cid < b.cid;
}
bool cmp1(Car a, Car b){
	return a.cid != b.cid ? a.cid < b.cid : a.time < b.time;
}
int main(){
	//freopen("in.txt", "r", stdin);
	string status;
	int n, k, i, h, m, s, time, j = 0, cnt = 0, maxt = -1, tot = 0;
	cin >> n >> k;
	for(i = 0; i<n; i++){
		cin >> car[i].cid;
		scanf("%d:%d:%d", &h, &m, &s);
		car[i].time = h*3600+m*60+s;
		cin >> status;
		car[i].status = status == "in" ? 1 : 0;
		car[i].flag = 0;
	}
	sort(car, car+n, cmp1);
	for(i = 1; i<n; i++){
		if(car[i].cid == car[i-1].cid && car[i-1].status == 1 && car[i].status == 0){
			car[i].flag = car[i-1].flag = 1;
			tot += 2;
		}
	}
	sort(car, car+n, cmp2);
	for(i = 0; i<=k; i++){
		if(i != k) {
			scanf("%d:%d:%d", &h, &m, &s);
			time = h*3600+m*60+s;	
		}
		else time = 23*3600+59*60+59;
		for(; j<tot && car[j].time <= time; j++){
			if(car[j].status == 1){
				if(!mp[car[j].cid]) {
					cnt++;	
				}
				mp[car[j].cid] = car[j].time+1;
			}
			else {
				if(mp[car[j].cid]){
					cnt--;
					stay[car[j].cid] += car[j].time+1-mp[car[j].cid]; 
					if(maxt < stay[car[j].cid]){
						maxt = stay[car[j].cid];
						res.clear();
						res.insert(car[j].cid);
					}
					else if(maxt == stay[car[j].cid])
						res.insert(car[j].cid);
					mp[car[j].cid] = 0;
				}
			}
		}
		if(i != k) printf("%d\n", cnt);
	} 
	for(auto it : res)
		printf("%s ", it.c_str());
	printf("%02d:%02d:%02d\n", maxt/3600, maxt/60%60, maxt%60);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值