1095. Cars on Campus (30)

1095. Cars on Campus (30)

时间限制
220 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代

题目意思:

给出N条记录,每条记录给出一辆车的车牌号,当前时间,进入还是出去。时间统一化成秒更方便计算。然后给出K和查询,每个查询给出一个时刻,输出在这个时刻校园内车的数量,最后输出停留时间最长的车的标号,如果有多辆一起输出。

注意:和1016题一样,需要去除无效数据,in和off必须配对,一辆车的一对in和off之间不允许在出现其他的in和off记录。

解题思路:

因为如果最长停留时间相同,则需要全部输出,那么很自然想到map映射。map<string,int>车的ID映射停留时间。中间我们可以定义两个结构体类型的vector数组,一个存放全部记录,一个存放有效记录。在找到有效记录之前,我们需要对所有记录进行一个排序,先按ID,再按时间。找到所有有效记录。同时记录下每辆车的停留时间,注意:是总停留时间,而不是单次停留时间。时间必须累加,就因为没累加,改了半个小时的错误,最大时间也是累计停留时间,而不是单次停留时间。在对有效数据进行一个时间的从前到后排序,然而如果我们每次都从前往后遍历肯定会超时,所以,我们可以定义一个数组,在遍历某辆车之前有多少辆车在校内。根据输入数据可以发现,查询的时间都是从前到后,所以我们可以将每次查询到的最后一辆车在数组中的位置记录下来,后面的查询直接从该位置开始查询即可。

下面是具体代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define N 10010
struct node{
  char id[10];
  int time;
  int status;
}temp;

bool cmp(node x,node y){
  if(strcmp(x.id,y.id) != 0){
    return strcmp(x.id,y.id)<0;
  }else{
    return x.time < y.time;
  }
}
bool cmp1(node x,node y){
  return x.time < y.time;
}
int main(){
  int n,k,h,m,s;
 // freopen("input.txt","r",stdin);
  scanf("%d%d\n",&n,&k);
  char str[10];
  vector<node>v,a(n);
  map<string,int>park;
  for(int i = 0;i < n;i++){
    scanf("%s %d:%d:%d %s\n",a[i].id,&h,&m,&s,str);
    a[i].time = h*3600+m*60+s;
    if(strcmp("in",str)==0){
      a[i].status = 1;
    }else{
      a[i].status = -1;
    }
  }
  sort(a.begin(),a.end(),cmp);
  int max = -1;
  for(int i = 0;i < n-1;i++){
    if(strcmp(a[i].id,a[i+1].id)==0&&a[i].status==1&&a[i+1].status==-1){
       
      
      park[a[i].id] += a[i+1].time - a[i].time;
      v.push_back(a[i]);
      v.push_back(a[i+1]);
	      if(park[a[i].id] >= max){
	        max = park[a[i].id];
	      }
      }
  }
  sort(v.begin(),v.end(),cmp1);
  vector<int>cnt(n);
  for(int i = 0;i < v.size();i++){
  	  if(i==0){
  		cnt[i] = v[i].status;
	  }else{
	  	cnt[i] = cnt[i-1]+v[i].status;
	  }
  }
  int index = 0;
  while(k--){
    scanf("%d:%d:%d",&h,&m,&s);
    int time = h*3600+m*60+s;
    int now = 0;
    int i;
    for(i = index;i < v.size();i++){
    	if(v[i].time > time){
    		printf("%d\n",cnt[i-1]);
    		break;
		}else{
			if(i==v.size()-1){
				printf("%d\n",cnt[i]);
			}
		}
	}
    index = i;
  }
  for(map<string,int>::iterator it = park.begin();it != park.end();it++){
    if(it->second == max){
      printf("%s ",it->first.c_str());
    }
  }
  printf("%02d:%02d:%02d\n",max/3600,max%3600/60,max%60);
  return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值