模拟专题
1095 Cars on Campus
配对要求是,如果一个车多次进入未出,取最后一个值;如果一个车多次out未进入,取第一个值。
注意:一个车可能出入校园好多次,停车的时间应该取之和
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <string> #include <string.h> #include <vector> #include <unordered_map> using namespace std; const int N=10010; const int MAXT=24*3600; int n,k,hh,mm,ss,flag,num,maxv; string p,st; unordered_map<string,int>plates; unordered_map<int,string>revplates; vector<string>ansstr; struct Car{ int tot,flag; bool operator<(Car x)const{ return tot<x.tot; } }; vector<Car>cars[N]; int timeList[MAXT]; int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;++i){ cin>>p; if(!plates.count(p)) plates[p]=++num; revplates[plates[p]]=p; scanf("%d:%d:%d",&hh,&mm,&ss); int tot=hh*3600+mm*60+ss; cin>>st; if(st=="in") flag=1; else flag=0; cars[plates[p]].push_back({tot,flag}); } for(int i=1;i<=num;++i){ sort(cars[i].begin(),cars[i].end()); int j=0,span=0; while(j<cars[i].size()){ while(!cars[i][j].flag&&j<cars[i].size()) j++; if(j>=cars[i].size()) break; while(cars[i][j].flag&&j<cars[i].size()) j++; if(j>=cars[i].size()) break; //cars[i][j-1].flag==1,cars[i][j].flag==0 timeList[cars[i][j-1].tot]++; timeList[cars[i][j].tot]--; span+=cars[i][j].tot-cars[i][j-1].tot; j++; } if(span>maxv){ ansstr.clear(); ansstr.push_back(revplates[i]); maxv=span; }else if(span==maxv){ ansstr.push_back(revplates[i]); } } for(int i=1;i<MAXT;++i) timeList[i]+=timeList[i-1]; sort(ansstr.begin(),ansstr.end()); while(k--){ scanf("%d:%d:%d",&hh,&mm,&ss); int tot=hh*3600+mm*60+ss; printf("%d\n",timeList[tot]); } for(int i=0;i<ansstr.size();++i) cout<<ansstr[i]<<" "; printf("%02d:%02d:%02d\n",maxv/3600,(maxv/60)%60,maxv%60); return 0; }
本文作者:Ryomk
本文链接:https://www.cnblogs.com/preccrep/p/16438834.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步