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
给出一些车辆进出的记录,要求给定一时间判断此时有多少车是in的状态(还没有out),末尾输出停留时间最多的车的编号(字母序),以及停留时间。
首先要排序才知道先后顺序,然后去除五匹配的in和out记录,把有效的记录单独存一下,并计算每一时刻处于in状态的车数,查询的时候二分。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include <map> using namespace std; ///所有的时间点都是在一天当中24小时制 不存在同一时刻一辆车又进又出 至少有完整一辆车的进出记录 ///没有对应out的in直接忽略 反过来也是 struct park { char pno[8],state[5];///车牌号 进出状态 int t,pnum;///对应时间 对应时间校内车辆 }c[10001],up[10001]; int n,k,num,ans,pnum,last[5001],nn,del[10001]; pair<string,int> car[5001]; bool cmp(park a,park b) { return a.t < b.t; } bool cmp1(pair<string,int> a,pair<string,int> b) { if(a.second == b.second)return a.first < b.first; return a.second > b.second; } int gettime(int h,int m,int s) { return h * 3600 + m * 60 + s; } int getnum(int t) {///二分求第一个大于当前时间的位置 if(t < up[0].t || t >= up[nn - 1].t)return 0;///始末位置车数都为0 因为去除了不匹配记录 所以不存在校内的车一直没有out的情况 int l = 0,r = nn - 1,mid; while(l < r) { mid = (l + r) / 2; if(up[mid].t > t)r = mid; else l = mid + 1; } return up[l - 1].pnum;///求得的是时间>t的位置 所以需要-1 } int main() { int h,m,s,t; char pno[8],state[4]; map<string,int> q; scanf("%d%d",&n,&k); for(int i = 0;i < n;i ++) { scanf("%s %d:%d:%d %s",c[i].pno,&h,&m,&s,c[i].state); c[i].t = gettime(h,m,s); } sort(c,c + n,cmp);///先按时间排序 for(int i = n - 1;i >= 0;i --) {///去除不匹配的 if(!q[c[i].pno]) {///第一次出现的车号 则给他分配一个位置 q[c[i].pno] = ++ num; last[num] = -1; car[num] = pair<string,int>(c[i].pno,0); } int d = last[q[c[i].pno]]; if(c[i].state[0] == 'o' && d != -1 && c[d].state[0] == 'o') { del[d] = 1;///表示无效的记录 } else if(c[i].state[0] == 'i' && (d == -1 || c[d].state[0] == 'i')) { del[i] = 1; } last[q[c[i].pno]] = i; } for(int i = 0;i < n;i ++) { if(del[i])continue; int d = last[q[c[i].pno]]; if(c[i].state[0] == 'o' && c[d].state[0] == 'o')continue; up[nn ++] = c[i];///记录正确的记录 last[q[c[i].pno]] = i; } for(int i = 0;i < nn;i ++) { if(up[i].state[0] == 'i') {///pnum记录校内停留车辆数目 pnum ++;///当前时间 校内车辆数加1 car[q[up[i].pno]].second -= up[i].t;///记录该车停留时间 out - in 所以这里是减去 } else { pnum --;///当前时间校内车辆数-1 car[q[up[i].pno]].second += up[i].t;///加上out } up[i].pnum = pnum;///当前时间校内车辆数更新 } sort(car + 1,car + num + 1,cmp1); for(int i = 0;i < k;i ++) { scanf("%d:%d:%d",&h,&m,&s); t = gettime(h,m,s); printf("%d\n",getnum(t)); } ans = car[1].second; printf("%s",car[1].first.c_str()); int i = 2; while(car[i].second == ans) { printf(" %s",car[i ++].first.c_str()); } printf(" %02d:%02d:%02d",ans / 3600,ans / 60 % 60,ans % 60); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!