1095. Cars on Campus (30)

题目:

Zhejiang University has 6 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 (<= 10000), the number of records, and K (<= 80000) 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

注意:
1、考察效率的题目,一定要让遍历次数最小。
2、一定要好好利用the queries are given in ascending order of the times这个条件。
3、先把每个plate的记录按照时间顺序来排好序,然后进行遍历,在计算每个需要求解的时间的car的个数时,遍历的起始位置都用上次遍历到的位置,因为the queries are given in ascending order of the times啊!另外在这里遍历的时候顺便把period算一下。
4、当然可能所给的时间点都算完了之后每个plate的记录可能并没有遍历完,所以还需要继续遍历完整,计算出period的总和。
5、把plate的name字符串转换成int有利于添加记录。

代码:
//1095
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<algorithm>
using namespace std;

struct record
{
	int time;//the time of this record
	int in;//the status of the plate
	record(int t,int i):time(t),in(i){}
	bool operator <(const record &r)const
	{
		return time<r.time;
	}
};

int name2id(string str)
{//transform the plate's name to a id number
	int num=0;
	for(int i=0;i<7;++i)
	{
		if(str[i]>='0'&&str[i]<='9')
			num = num*36+str[i]-'0';
		else
			num = num*36+str[i]-'A'+10;
	}
	return num;
}

int main()
{
	int n,k;
	scanf("%d%d",&n,&k);
	map<int,int>id2idx;//the id of plate to its index
	vector<string>platename;//the name of each plate
	vector<vector<record>>all;//all record
	for(int i=0;i<n;++i)
	{
		string name;
		char status[5];
		int hour,min,sec;
		cin>>name;
		scanf("%d:%d:%d %s",&hour,&min,&sec,status);
		record r(hour*3600+min*60+sec,status[0]=='i'?1:0);
		int id=name2id(name);
		if(id2idx.count(id))
			all[id2idx[id]].push_back(r);
		else
		{//insert it of it is a new plate
			id2idx.insert(pair<int,int>(id,all.size()));
			platename.push_back(name);
			vector<record>rec;
			rec.push_back(r);
			all.push_back(rec);
		}
	}
	for(int i=0;i<all.size();++i)
		sort(all[i].begin(),all[i].end());
	vector<int>cur;//current index
	cur.assign(all.size(),0);
	vector<int>period;
	period.assign(all.size(),0);
	for(int i=0;i<k;++i)
	{
		int hour,min,sec;
		scanf("%d:%d:%d",&hour,&min,&sec);
		int t=hour*3600+min*60+sec;
		int count=0;//the number of cars on plate at this time
		for(int j=0;j<all.size();++j)
		{
			while(1)
			{
				//if this time is earlier than the earlist record of this plate
				//or later than the last record of this plate
				if((cur[j]==0&&t<all[j][0].time) || cur[j]>=all[j].size()-1)
					break;
				if(t>=all[j][cur[j]].time && t<all[j][cur[j]+1].time)
				{//if this time is just between the current record and next recod
					if(all[j][cur[j]].in && !all[j][cur[j]+1].in)
						++count;
					break;
				}
				//calculate the period of each plate
				if(all[j][cur[j]].in && !all[j][cur[j]+1].in)
					period[j] += all[j][cur[j]+1].time-all[j][cur[j]].time;
				++cur[j];
			}
		}
		printf("%d\n",count);
	}
	int maxperiod=0;
	for(int i=0;i<all.size();++i)
	{
		while(cur[i]<all[i].size()-1)
		{//calculate the period of the rest recods
			if(all[i][cur[i]].in && !all[i][cur[i]+1].in)
				period[i] += all[i][cur[i]+1].time-all[i][cur[i]].time;
			++cur[i];
		}
		if(period[i]>maxperiod)
			maxperiod=period[i];
	}
	for(int i=0;i<all.size();++i)
		if(maxperiod==period[i])
			cout<<platename[i]<<' ';
	printf("%02d:%02d:%02d",maxperiod/3600,(maxperiod%3600)/60,maxperiod%60);
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值