pat 1095 Cars on Campus

来源:互联网 发布:淘宝祛痘产品有用吗 编辑:程序博客网 时间:2024/06/06 13:04

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 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:
1452101

JH007BD ZD00001 07:20:09

#include <iostream>#include <cstdio>#include <map>#include <vector>#include <algorithm>#include <string>using namespace std;struct Car{int time;string name;int status; // 0 stands for out, 1 stands for inCar(int tm, int sta): time(tm), status(sta) {}bool operator < (const Car& car) const {return time < car.time;}};int name2id(string str) {int sum = 0;for (int i = 0; i < str.length(); ++i){if(str[i] >= '0' && str[i] <= '9') {sum = sum*36 + str[i]-'0';} else {sum = sum*36+str[i]-'A'+10;}}return sum;}int main() {int n, m;string name, status;int hour, minute, seconds;map<int, int> hash;vector<vector<Car> > allCars;vector<string> carName;cin >> n >> m;for (int i = 0; i < n; ++i){cin >> name;scanf("%d:%d:%d", &hour, &minute, &seconds);cin >> status;int times = hour*3600+minute*60+seconds;Car car(times, status[0]=='i'?1:0);car.name = name;int num = name2id(name);if(hash.count(num)) {allCars[hash[num]].push_back(car);} else {vector<Car> temp;temp.push_back(car);hash.insert(pair<int, int>(num, hash.size()));allCars.push_back(temp);}}for (int i = 0; i < allCars.size(); ++i){sort(allCars[i].begin(), allCars[i].end());}vector<int> cur(allCars.size(), 0);vector<int> duration(allCars.size(), 0);for (int i = 0; i < m; ++i){scanf("%d:%d:%d", &hour, &minute, &seconds);int times = hour*3600+minute*60+seconds;int ans = 0;for (int j = 0; j < allCars.size(); ++j){while(true) {if(cur[j] >= allCars[j].size()-1 || allCars[j][cur[j]].time > times) {break;}if(times >= allCars[j][cur[j]].time && times < allCars[j][cur[j]+1].time) {if(allCars[j][cur[j]].status == 1 && allCars[j][cur[j]+1].status == 0) {ans++;break;}}if(allCars[j][cur[j]].status == 1 && allCars[j][cur[j]+1].status == 0) {duration[j] += allCars[j][cur[j]+1].time-allCars[j][cur[j]].time;}cur[j]++;}}cout << ans << endl;}for (int i = 0; i < allCars.size(); ++i){while(cur[i] < allCars[i].size()-1) {if(allCars[i][cur[i]].status == 1 && allCars[i][cur[i]+1].status == 0) {duration[i] += allCars[i][cur[i]+1].time - allCars[i][cur[i]].time;}cur[i]++;}}int maxx = duration[0];carName.push_back(allCars[0][0].name);for (int i = 1; i < duration.size(); ++i){if(duration[i] > maxx) {maxx = duration[i];carName.clear();carName.push_back(allCars[i][0].name);} else if(duration[i] == maxx){carName.push_back(allCars[i][0].name);}}sort(carName.begin(), carName.end());for (int i = 0; i < carName.size(); ++i){cout << carName[i];//if(i != carName.size()-1)cout << " ";}printf("%02d:%02d:%02d\n", maxx/3600, maxx%3600/60, maxx%60);return 0;}


0 0