Topic Links:
PAT1095
Test instructions
There is a school, there will be a lot of cars in and out every day, give n vehicles in and out of the situation and M inquiry
Each query output the current campus how many cars, the time from 0 points 24 points last output the longest stay of the car's license plate number and dwell time, if there are multiple, then the dictionary order output license plate number
Note that all data must be made to guarantee the following conditions, otherwise invalid data:
1 at first, there was no car on campus.
2 Finally, there's no car to stop.
3 The same car comes in multiple times, only the last one is right.
4 the same car went out several times, and only the first one was right.
Solution:
Review of basic Skills
Use a map to record the subscript of each vehicle grade
First, all statements are sorted by time and all query times are saved
Iterate through the deletion of an illegal statement
Iterate over one more time set up an inquiry pointer
Outputs the current number of vehicles when the time meets the requirements
Use set to save all the longest-staying license plate numbers
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <set># Include<algorithm> #define MAXN 10050using namespace std;struct node2//status of each vehicle {string id; license plate int State; 1:in 0:out int sum; int in_time; int Loc; The position of the previous in statement} id[maxn];struct node//All statement information {int time; String ID; int state; int flag; Whether the statement is correct} s[maxn];map<string,int>q;set<string>w;int CMP (node A,node b) {return a.time<b.time;} int main () {//Freopen ("In.txt", "R", stdin); int n,m; int query[maxn*8]; String str1,str2; scanf ("%d%d", &n,&m); for (int i=1; i<=n; i++) {cin>>s[i].id>>str1>>str2; s[i].flag=0; S[i].time= ((str1[0]-' 0 ') *10+str1[1]-' 0 ') *3600+ ((str1[3]-' 0 ') *10+str1[4]-' 0 ') *60+ ((str1[6]-' 0 ') *10+str1[7]-' 0 ') ; S[i].state=str2.compare ("in") ==0?1:0; } for (int i=1; i<=m; i++) {cin>>str1; query[i]= ((str1[0]-' 0 ') *10+str1[1]-' 0 ') *3600+ ((str1[3]-' 0 ') *10+str1[4]-' 0 ') *60+ ((str1[6]-' 0 ') *10+str1[7]-' 0 '); } sort (s+1,s+1+n,cmp); int num=1,que=1,ss=0,d; int ans=0; for (int i=1; i<=n; i++)//mark the wrong statement {str1=s[i].id; if (q[str1]==0)//The current license plate does not appear {id[num]= {s[i].id,s[i].state,0,s[i].time,i}; q[str1]=num++; } else {D=Q[STR1]; if (id[d].state==1&&s[i].state==1)//Multiple in takes the last preceding in statement not valid {s[id[d].loc].flag=1; Id[d].loc=i; } else if (id[d].state==1&&s[i].state==0)//go out id[d].state=0; else if (id[d].state==0&&s[i].state==1)//Enter {id[d].state=1 again; Id[d].loc=i; } else if (id[d].state==0&&s[i].state==0)//Multiple out takes the first s[i].flag=1; }} for (int i=1;i<num;i++) {if (id[i].state==1)//Enter not come out illegal {S[ID[I].LOC].FL ag=1; id[i].state=0; }} for (int i=1; i<=n; i++)//Traverse All statements {if (s[i].flag) continue; while (Que<m+1&&query[que]<s[i].time)//while remember {que++; printf ("%d\n", SS); } str1=s[i].id; D=Q[STR1]; if (id[d].state==1&&s[i].state==0)//Enter out {ss--; id[d].state=0; Id[d].sum+=s[i].time-id[d].in_time; if (Id[d].sum>ans) {ans=id[d].sum; W.clear (); W.insert (id[d].id); } else if (Id[d].sum==ans) W.insert (id[d].id); } else if (id[d].state==0&&s[i].state==1)//Enter {ss++ again; id[d].state=1; Id[d].in_time=s[i].time; }} while (Que<m+1) {printf ("%d\n", SS); que++; } Set<string>::iterator II; For (Ii=w.begin (); Ii!=w.end (); ii++) cout<<*ii<< ""; printf ("%02d:%02d:%02d\n", ans/3600,ans%3600/60,ans%60); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
PAT 1095 Cars on Campus simulation