PAT甲级 1095 Cars on Campus (测试点4超时)

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 (≤104), the number of records, and K (≤8×104) 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

分析:

一开始的思路,是将记录全部进行排序,找到有效对,再按车子为单位进行记录,这样写了提交上去意料之中的测试点4超时了。原因就是在时间查询,但是写的原代码实在很难进行直接的查询改进,遂改写(写了很久,所以还是想贴出来)。

对之前代码可以改的地方很多,首先是时间,可以把全部的时间转成秒,在后面的判断就可以更加方便。其次不需要以车子为单位进行记录,只要把有效的记录放到新的valid数组中。时间的查询,这里要利用查询的时间是按大小排序这个条件,先将所有的有效记录仅按时间大小排序,从小到大和要查询的时间进行比较,如果该记录小于等于查询时间,是in则计数量++,是out则计数量--。这个计数量是累积的,所以一个时间查询完就输出。最后,因为输出最大停留时间时是要按字典序排的,记得按字典序排回来。

注意点:如果是in的时间刚好和查询时间相等,需要算到该时刻的停车量,但是out和查询时间相等,不需要算。

代码:

超时代码:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int jishu[80005];
struct Time{
	int hour;
	int minute;
	int second;
}T[80005];
struct record{
	string id;
	Time t;
	string status;
}c[10005];//记录
struct Car{
	string id;
	vector<Time> tin;//in的时间记录
	vector<Time> tout;//out的时间记录
	int sum;//停的总时间,按秒计算
}car[10005];//按车子进行记录
bool compare(record a,record b){
	if(a.id!=b.id)
	return a.id<b.id;
	if(a.t.hour!=b.t.hour)
	return a.t.hour<b.t.hour;
	if(a.t.minute!=b.t.minute)
	return a.t.minute<b.t.minute;
	if(a.t.second!=b.t.second)
	return a.t.second<b.t.second;
	return a.status<b.status;
}
bool comparetime(Time a,Time b){
	if(a.hour!=b.hour)
	return a.hour<b.hour;
	if(a.minute!=b.minute)
	return a.minute<b.minute;
	return a.second<b.second;
}
int jisuan(Time a,Time b){//根据out和in计算停的时间
	int zong;
	zong=b.hour*3600+b.minute*60+b.second-(a.hour*3600+a.minute*60+a.second);
	return zong;
}
Time zhuanhuan(int sum){
	Time a;
	a.hour=sum/3600;
	a.minute=sum/60%60;
	a.second=sum%60;
	return a;
}
void shuchu(Time a){
	printf("%02d:%02d:%02d",a.hour,a.minute,a.second);
}
int main(){
	int n,k;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>c[i].id;
		scanf("%d:%d:%d",&c[i].t.hour,&c[i].t.minute,&c[i].t.second);
		cin>>c[i].status;
	}
	for(int i=0;i<k;i++){
		scanf("%d:%d:%d",&T[i].hour,&T[i].minute,&T[i].second);
	}
	sort(c,c+n,compare);
	int pi=0;//能匹配的个数 
	int mmax=0;
	for(int i=1;i<n;i++){
		if(c[i].status=="out"){//该位需要是out才能和前面匹配 
			if(c[i-1].id==c[i].id&&c[i-1].status=="in"){
				int x;//判断是哪辆车 
				bool flag=false;
				for(int j=0;j<pi;j++){
					if(c[i].id==car[j].id){
						flag=true;
						x=j;
						break;
					}
				}
				if(flag==false){
					x=pi;
					pi++;
				}
				car[x].id=c[i].id;
				car[x].tin.push_back(c[i-1].t);
				car[x].tout.push_back(c[i].t);
				if(car[x].tin.size()==1){
					car[x].sum=0;
				}
				car[x].sum+=jisuan(c[i-1].t,c[i].t);
				if(car[x].sum>mmax){
					mmax=car[x].sum;
				}
				i+=1;
		    }
	    }
	}
    for(int i=0;i<k;i++){
    	for(int j=0;j<pi;j++){
    		for(int z=0;z<car[j].tin.size();z++){
    			if((car[j].tin[z].hour==T[i].hour&&car[j].tin[z].minute==T[i].minute&&car[j].tin[z].second==T[i].second||comparetime(car[j].tin[z],T[i]))&&comparetime(T[i],car[j].tout[z])){
    				jishu[i]++;
				}else if(!comparetime(car[j].tin[z],T[i])){//如果in的时间超过改询问时间,就结束该车的查询
					break;
				}
			}
		}
	}
	for(int i=0;i<k;i++){
		cout<<jishu[i]<<endl;
	}
	for(int i=0;i<pi;i++){
		if(car[i].sum==mmax){
			cout<<car[i].id<<" ";
		}
	}
	shuchu(zhuanhuan(mmax));
}

正确代码:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Time{
	int hour;
	int minute;
	int second;
}T;
struct record{
	string id;
	int s;
	string status;
	int sum;
}c[10005],valid[10005];//记录和有效记录 
bool compare(record a,record b){
	if(a.id!=b.id)
	return a.id<b.id;
	if(a.s!=b.s)
	return a.s<b.s;
	return a.status<b.status;
}
bool compare2(record a,record b){
	return a.s<b.s;
}
int jisuan(Time b){//转换成秒 
	int zong;
	zong=b.hour*3600+b.minute*60+b.second;
	return zong;
}
Time zhuanhuan(int sum){//转换成时间格式 
	Time a;
	a.hour=sum/3600;
	a.minute=sum/60%60;
	a.second=sum%60;
	return a;
}
void shuchu(Time a){
	printf("%02d:%02d:%02d",a.hour,a.minute,a.second);
}
int main(){
	int n,k;
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>c[i].id;
		scanf("%d:%d:%d",&T.hour,&T.minute,&T.second);
		cin>>c[i].status;
		c[i].s=jisuan(T);
	}
	sort(c,c+n,compare);
	for(int i=0;i<n;i++){
		c[i].sum=0;
	}
	int pi=0;//能匹配的个数 
	int mmax=0;
	for(int i=1;i<n;i++){
		if(c[i].status=="out"){//该位需要是out才能和前面匹配 
			if(c[i-1].id==c[i].id&&c[i-1].status=="in"){
				valid[pi]=c[i-1];
				valid[pi+1]=c[i];
				if(pi!=0&&valid[pi+1].id==valid[pi-1].id){
					valid[pi+1].sum=valid[pi-1].sum+valid[pi+1].s-valid[pi].s;
				}else{
					valid[pi+1].sum=valid[pi+1].s-valid[pi].s;
				}
				if(valid[pi+1].sum>mmax){
					mmax=valid[pi+1].sum;
				}
				pi+=2;
				i+=1;
		    }
	    }
	}
	sort(valid,valid+pi,compare2);//按时间排序 
    int j=0;
    int jishu;
    for(int i=0;i<k;i++){
    	scanf("%d:%d:%d",&T.hour,&T.minute,&T.second);
    	int shijian=jisuan(T);
    	while(j<pi&&valid[j].s<=shijian){
    		if(valid[j].status=="in"){
				jishu++;
			}else{
				jishu--;
			}
			j++;
		}
		cout<<jishu<<endl;
	}
	sort(valid,valid+pi,compare);//因为输出id要按字典序,所以这里排回来 
	for(int i=1;i<pi;i+=2){
		if(valid[i].sum==mmax){
			cout<<valid[i].id<<" ";
		}
	}
	shuchu(zhuanhuan(mmax));
}

总结: 

在需要进行时间计算、时间比较的时候可以直接转成秒。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值