您的位置:首页 > 其它

1095. Cars on Campus (30)

2018-03-15 10:55 246 查看

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
code:#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

struct Car{
string plate_num;
bool valid;
int inTime;
int outTime;
int total;
Car(){
valid = false;
inTime = outTime = -1;
total = 0;
}
bool operator < (const Car& other)const{
if(total == other.total)
return plate_num < other.plate_num;

4000
return total > other.total;
}
};

struct Record{
string plate_num;
int time;
string status;
Record(string plate,int t,string s){
plate_num = plate;
time = t;
status = s;
}
bool operator < (const Record& other)const{
return time < other.time;
}
};
struct QueryNode{
int time;
int value;
QueryNode(int t,int v){
time = t;
value = v;
}
bool operator < (const QueryNode& other)const{
return time < other.time;
}
};
int main(){
map<string,Car> carMap;
vector<Record> records;
vector<QueryNode> queryNodes;
int N,K;
int h,m,s;
cin >> N >> K;
for(int i = 0; i < N; i++){
string plate_num;
string status;
cin >> plate_num;
scanf("%d:%d:%d",&h,&m,&s);
cin >> status;
records.push_back(Record(plate_num,h*3600+m*60+s,status));
}
sort(records.begin(),records.end());//将所有信息储存下来并按照时间顺序排好序
//筛选有效信息对,和按照时间顺序放入查询向量中
for(int i = 0; i < records.size(); i++){
Record &r = records[i];
if(r.status == "in"){
if(!carMap.count(r.plate_num)){
Car c = Car();
c.plate_num = r.plate_num;
c.inTime = r.time;
carMap[r.plate_num] = c;
}
else{
Car &c = carMap[r.plate_num];
c.inTime = r.time;
}
}
else{
if(!carMap.count(r.plate_num)){
Car c = Car();
c.plate_num = r.plate_num;
c.outTime = r.time;
carMap[r.plate_num] = c;
}
else{
Car &c = carMap[r.plate_num];
c.outTime = r.time;
if(c.inTime != -1 && c.inTime < c.outTime){
QueryNode inQ = QueryNode(c.inTime,1);
QueryNode outQ = QueryNode(c.outTime,-1);
queryNodes.push_back(inQ);
queryNodes.push_back(outQ);
c.total += c.outTime - c.inTime;
c.inTime = c.outTime = -1;
c.valid = true;
}
}
}
}
//记录有效汽车信息,并按照停留时间长短排序
vector<Car> validCars;
map<string,Car>::iterator it;
for(it = carMap.begin(); it != carMap.end(); it++){
Car &c = it->second;
if(c.valid){
Car c = it->second;
validCars.push_back(c);
}
}
sort(queryNodes.begin(),queryNodes.end());
int cnt = 0;
int cur = 0;
for(int i = 0; i < K; i++){
scanf("%d:%d:%d",&h,&m,&s);
int t = h * 3600 + m * 60 + s;
while(cur < queryNodes.size()){
QueryNode q = queryNodes[cur];
if(q.time <= t){
cnt += q.value;
cur++;
}
else break;
}
printf("%d\n",cnt);
}
//输出
sort(validCars.begin(),validCars.end());
int maxTotal = validCars[0].total;
for(int i = 0; i < validCars.size(); i++){
if(validCars[i].total != maxTotal) break;
cout << validCars[i].plate_num << " ";
}
h = maxTotal / 3600;
m = (maxTotal - h * 3600) / 60;
s = maxTotal % 60;
printf("%02d:%02d:%02d\n",h,m,s);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: