1095. Cars on Campus (30)

来源:互联网 发布:有关大数据的例子 编辑:程序博客网 时间:2024/04/30 12:38
题目链接:http://www.patest.cn/contests/pat-a-practise/1095
题目:

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 ascendingorder 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 7JH007BD 18:00:01 inZD00001 11:30:08 outDB8888A 13:00:00 outZA3Q625 23:59:50 outZA133CH 10:23:00 inZD00001 04:09:59 inJH007BD 05:09:59 inZA3Q625 11:42:01 outJH007BD 05:10:33 inZA3Q625 06:30:50 inJH007BD 12:23:42 outZA3Q625 23:55:00 inJH007BD 12:24:23 outZA133CH 17:11:22 outJH007BD 18:07:01 outDB8888A 06:30:50 in05:10:0006:30:5011:00:0012:23:4214:00:0018:00:0023:59:00
Sample Output:
1452101JH007BD ZD00001 07:20:09

分析:
汽车的出入记录,要对记录做处理:首先是对时间的处理Time2int,对名字的处理两个map互相映射string和int,然后个人认为比较好的方法是把记录按车牌号分到各个vector中,再按时间排序,挑选出成对的记录,记录每个车辆的累计停车时间,得出最长停车时间的车。
然后再把所有有效的记录都放入一个vector中(某时刻停车的数量已经和车牌无关了)。在对这个vector进行时间排序,根据in还是out就可以判断出某一时刻的车辆数。

AC代码:
#include<cstdio>#include<iostream>#include<vector>#include<algorithm>#include<queue>#include<stack>#include<string>#include<string.h>#include<map>using namespace std;struct Record{ int time; bool status; Record(int t,int s):time(t),status(s){}};bool cmp(Record* r1, Record* r2){ return r1->time < r2->time;}int Time2int(int h, int m, int s){ return h * 60 * 60 + m * 60 + s;}vector<vector<Record*>>V;map<string, int>M;map<int, string>M2;int main(){ freopen("F://Temp/input.txt", "r", stdin); int record_num, query_num; cin >> record_num >> query_num; int M_idx = 0; for (int i = 0; i < record_num; ++i){  string str;  int hour, min, sec;  string sta;  cin >> str;  scanf("%d:%d:%d", &hour, &min, &sec);  cin >> sta;  Record *rec = new Record(Time2int(hour,min,sec),sta == "in"?true:false);  if (M.find(str) == M.end()){   M2[M_idx] = str;   M[str] = M_idx++;   vector<Record*>VR;   VR.push_back(rec);   V.push_back(VR);  }  else   V[M[str]].push_back(rec); } int *sum_time = new int[V.size()]; for (int i = 0; i < V.size(); ++i){  sum_time[i] = 0; } vector<Record*>VT; for (int i = 0; i < V.size(); ++i){  sort(V[i].begin(), V[i].end(), cmp);  int j = 0;  //vector<Record*>V_tmp;  for (int j = 0; j < V[i].size() - 1; j++){   if (V[i][j]->status == true && V[i][j + 1]->status == false){//in和out成对出现    //V_tmp.push_back(V[i][j]);    //V_tmp.push_back(V[i][j + 1]);    VT.push_back(V[i][j]);    VT.push_back(V[i][j + 1]);    sum_time[i] += V[i][j + 1]->time - V[i][j]->time;//顺便把每辆车时间算出来   }  }  //V[i] = V_tmp;//整理出有用的成对的记录 } //因为查询很多,所以可以把汽车整理出的记录集中排序,然后按照时间来做一个循环 sort(VT.begin(), VT.end(), cmp); //for (int i = 0; i < VT.size(); ++i){ //int tt = VT[i]->time; //cout << tt / 3600 << ":" << tt / 60 % 60 << ":" << tt % 60 << " "; //if (VT[i]->status) // cout << "in" << endl; //else // cout << "out" << endl; //} int *car_sum = new int[VT.size()]; car_sum[0] = 1; for (int i = 1; i < VT.size(); ++i){  if (VT[i]->status == true)car_sum[i] = car_sum[i - 1] + 1;  else car_sum[i] = car_sum[i - 1] - 1; } int time_idx = 0;//用来指示上一个查询的时间位置在哪里 for (int i = 0; i < query_num; ++i){  int hour, min, sec;  scanf("%d:%d:%d", &hour, &min, &sec);  int query_t = Time2int(hour, min, sec);  while (time_idx < VT.size() && VT[time_idx]->time <= query_t)time_idx++;  cout << car_sum[time_idx - 1]<< endl; }  //汽车各自的记录可以找出哪一辆车是停得最长的 int max_time = 0; vector<int>V_max_idx; for (int i = 0; i < V.size(); ++i){  if (sum_time[i] > max_time){   V_max_idx.clear();   V_max_idx.push_back(i);   max_time = sum_time[i];  }  else if (sum_time[i] == max_time){   V_max_idx.push_back(i);  } } for (int i = 0; i < V_max_idx.size(); ++i){  cout << M2[V_max_idx[i]] << " "; } printf("%02d:%02d:%02d", max_time / 3600, max_time / 60 % 60, max_time % 60); cout << endl; return 0;}


截图:

——Apie陈小旭
0 0
原创粉丝点击