题意
给出n条车辆出入的记录,按时间顺序一一匹配。然后给出k个时间点,找到这k个时间点对应校园内车辆的数量。最后输出停留时间最长的车名。
思路
- 设计一种数据结构node,存储输入数据放入rec数组。
- sort按(名字)时间顺序排序。
- 找能匹配的记录(in - out),记录进出时间点分别对t_tag++or–,记录时间差存入rec_t,同时更新最大停车时间maxt。
- 分析一天有3600 * 24 = 86400(1E5),所以可以通过前缀和的方法,确定每一时刻校园内部停车数量。t_tag数组记录某一秒进出校园数量,sum记录当前校园车辆
注意1:the chronologically next record - 按时间顺序的下一个记录。这里理解了就知道可以按时间排好序后,找到一前一后相同name的进出记录。即:
for(int i = 0; i < que.size() - 1; i ++)
if(que[i].name == que[i + 1].name && que[i].sta == 1 && que[i + 1].sta == 0)
注意2:结构体的定义方式:
struct node{
string name;
int time;
bool sta;
node(string a, int b, bool c): name(a), time(b), sta(c){};
bool operator < (const node A) const{
return name != A.name ? name < A.name : time < A.time;
}
};
技巧:为了结构清晰,可以设置node与rec为如下结构(解法2):
struct node{
int time;
string status;
};
map<string, deque<node>> nodes;
问题:没找到一种数据结构,能支持O(1)时间复杂度删除+随机访问。
解法1
#include<bits/stdc++.h>
using namespace std;
int t_tag[100000], sum[100000];
int n, k, h, m, s;
string name, sta;
map<string, int> rec_t;
deque<node> que;
struct node{
string name;
int time;
bool sta;
node(string a, int b, bool c): name(a), time(b), sta(c){};
bool operator < (const node A) const{
return name != A.name ? name < A.name : time < A.time;
}
};
int main(){
cin>>n>>k;
while(n--){
cin>>name;
scanf("%d:%d:%d", &h, &m, &s);
cin>>sta;
node tmp(name, h * 3600 + m * 60 + s, sta == "in" ? true : false);
que.push_back(tmp);
}
sort(que.begin(), que.end());
int maxt = -1;
for(int i = 0; i < que.size() - 1; i ++){
if(que[i].name == que[i + 1].name && que[i].sta == 1 && que[i + 1].sta == 0){
t_tag[que[i].time]++;
t_tag[que[i + 1].time]--;
rec_t[que[i].name] += que[i + 1].time - que[i].time;
maxt = max(rec_t[que[i].name], maxt);
}
}
sum[0] = t_tag[0];
for(int i = 1; i <= 3600 * 24; i++)
sum[i] = sum[i - 1] + t_tag[i];
while(k--){
scanf("%d:%d:%d", &h, &m, &s);
cout<<sum[h * 3600 + m * 60 + s]<<endl;
}
for(auto it : rec_t)
if(it.second == maxt)
cout<<it.first<<' ';
printf("%02d:%02d:%02d",maxt/3600, (maxt%3600)/60, maxt%60);
return 0;
}
解法2
#include<bits/stdc++.h>
using namespace std;
struct node{
int t;
string status;
};
bool cmp(node a,node b){
return a.t<b.t;
}
int main(){
int n,k,max_t=0;cin>>n>>k;
unordered_map<string,deque<node>> nodes;
set<string> output;
string id,status;
int hh,mm,ss;
for(int i=0;i<n;i++){
cin>>id;
scanf("%d:%d:%d",&hh,&mm,&ss);
cin>>status;
nodes[id].push_back({hh*3600+mm*60+ss,status});
}
for(auto & i:nodes){//!!!!重点,auto一般是复制,要修改原数据需要auto后面+&
deque<node> ans;
sort(i.second.begin(),i.second.end(),cmp);
int j=1,sum_t=0;
while(j<i.second.size()){
if(i.second[j].status=="out"&&i.second[j-1].status=="in"){
ans.push_back(i.second[j-1]);
ans.push_back(i.second[j]);
sum_t+=i.second[j].t-i.second[j-1].t;
j+=2;
}else
j++;
}
nodes[i.first]=ans;
if(!output.size()||max_t<sum_t){
output.clear();
output.insert(i.first);
max_t=sum_t;
}else if(max_t==sum_t)
output.insert(i.first);
}
vector<int> time(k), count(k);
for(int i=0;i<k;i++){
scanf("%d:%d:%d",&hh,&mm,&ss);
time[i]=hh*3600+mm*60+ss;
}
for(auto car:nodes){//查看每个车子进出记录
int ptr=0;
for(int j=1;j<car.second.size();j+=2){//j=node
while(ptr<k&&car.second[j].t>time[ptr]){
if(car.second[j-1].t<=time[ptr])
count[ptr]++;
ptr++;
}
if(ptr==k){
ptr=0;
break;
}
}
}
for(auto i:count)
cout<<i<<endl;
for(auto i:output)
cout<<i<<' ';
printf("%02d:%02d:%02d",max_t/3600,max_t%3600/60,max_t%60);
return 0;
}
原题描述
Zhejiang University has 8 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 (≤104), the number of records, and K (≤8×104) 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