P3. Cars on Campus (30) /深入理解数据利用率!!!

来源:互联网 发布:淘宝女装街拍摄影技巧 编辑:程序博客网 时间:2024/06/05 08:11
P3 Cars on Campus   (20分)

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 integersNNN (≤104\le 10^4104), the number of records, and KKK (≤8×104\le 8\times 10^48×104) the number of queries. Then NNN 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 being00:00:00 and the latest23:59:59; and status is eitherin 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 anout record. Anyin records that are not paired with anout record are ignored, as areout records not paired with anin record. It is guaranteed that at least one car is well paired in the input, and no car is bothin andout at the same moment. Times are recorded using a 24-hour clock.

Then KKK lines of queries follow, each gives a time point in the format hh:mm:ss. Note: the queries are given inaccending 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 7JH007BD 18:00:01 inZD00001 11:30:08 outDB8888A 13:00:00 outZA3Q625 23:59:50 outZA133CH 10:23:00 inZD00001 04:09:59 inJH007BD 05:09:59 inZA3Q625 11:42:01 outJH007BD 05:10:33 inZA3Q625 06:30:50 inJH007BD 12:23:42 outZA3Q625 23:55:00 inJH007BD 12:24:23 outZA133CH 17:11:22 outJH007BD 18:07:01 outDB8888A 06:30:50 in05:10:0006:30:5011:00:0012:23:4214:00:0018:00:0023:59:00

Sample Output:

1452101JH007BD ZD00001 07:20:09
#include<iostream>#include<vector>#include<string>#include<cstdio>#include<algorithm>#include<map>using namespace std;int tim[86401]={0};int ans[86401]={0};int t_max=0;vector<string> answer;class ca{public:string name;int state;     //0=in,1=outint time;int t_total;ca* Next;};ca car[10001];map<string,ca> carstring;class ti{public:int h;int m;int s;void disp(){printf("%02d:%02d:%02d\n",h,m,s);}};bool compare1(ca a,ca b){if(a.time<b.time)return true;return false;}ti exchange(int t){ti t1;int h,m,s;t1.h=t/3600;t1.m=(t-(t1.h)*3600)/60;t1.s=t%60;return t1;}int exchange(ti t){return t.h*3600+t.m*60+t.s;}int main(){int N,M;cin>>N>>M;string s;int i;ca *record=new ca[N];for(i=0;i<10001;i++){car[i].Next=NULL;car[i].state=0;car[i].time=0;car[i].t_total=0;}ti temp;for(i=0;i<N;i++){cin>>record[i].name;scanf("%d:%d:%d",&temp.h,&temp.m,&temp.s);record[i].time=exchange(temp);cin>>s;if(s=="in")record[i].state=0;elserecord[i].state=1;}sort(record,record+N,compare1);//先用快速排序把输入按时间排起来,至少我现在还没有想到能不排序就能运行出来而且比排序更快的算法ca c;map<string,ca>::iterator it;//定义迭代器for(i=0;i<N;i++){it=carstring.find(record[i].name);if(it!=carstring.end()){c=record[i];if(it->second.state==0&&c.state==1){tim[it->second.time]++;//这个(有点类似积分)想法可以说充分利用了数据,用空间代价代替时间代价tim[c.time]--;//刚开始的想法是做个循环在开始和结束之间的tim值都加上1it->second.t_total+=(c.time-it->second.time);if(it->second.t_total>t_max){t_max=it->second.t_total;answer.clear();answer.push_back(it->second.name);//求最大值}else if(it->second.t_total==t_max){answer.push_back(it->second.name);}it->second.time=c.time;it->second.state=1;}else if(it->second.state==0&&c.state==0){it->second.time=c.time;}else if(it->second.state==1&&c.state==0){it->second.state=0;it->second.time=c.time;}}else{if(record[i].state==0)carstring.insert(map<string,ca>::value_type (record[i].name,record[i]));}}//hash_insert(record[i])//如果不用STL的map,把这句代替掉上面循环的内容,然后把下面的函数去注int ss=0;for(i=0;i<86400;i++){ss+=tim[i];ans[i]+=ss;}for(i=0;i<M;i++){scanf("%d:%d:%d",&temp.h,&temp.m,&temp.s);printf("%d\n",ans[exchange(temp)]);}sort(answer.begin(),answer.end());for(i=0;i<answer.size();i++){cout<<answer[i]<<" ";}if(t_max!=0)exchange(t_max).disp();//exchange(3699).disp();}/*int hash_change(string s){//哈希函数int t=1;if(s[1]>'A')t*=s[1]-'A';elset*=s[1]-'0';if(s[5]>'A')t*=s[5]-'A';elset*=s[5]-'0';if(s[6]>'A')t*=s[6]-'A';elset*=s[6]-'0';t+=(s[0]-'0')*(s[2]-'0');return t%10000;}void hash_insert(ca c){//链表式哈希表实现int t;int i;t=hash_change(c.name);ca *r;if(car[t].name.empty()&&c.state==0){car[t].name=c.name;car[t].time=c.time;}else{if(!car[t].name.empty()){r=&car[t];while(r->name!=c.name){if(r->name==c.name)break;//cout<<r->name<<"!="<<c.name<<endl;if(r->Next==NULL){if(c.state==0){r->Next=new ca;r=r->Next;r->Next=NULL;r->name=c.name;r->time=c.time;r->t_total=0;r->state=0; }else{return; }}else{r=r->Next;}}if(r->time!=c.time){if(r->state==0&&c.state==1){tim[r->time]++;tim[c.time]--;r->t_total+=(c.time-r->time);if(r->t_total>t_max){t_max=r->t_total;answer.clear();answer.push_back(r->name);}else if(r->t_total==t_max){answer.push_back(r->name);}r->time=c.time;r->state=1;}else if(r->state==0&&c.state==0){r->time=c.time;}else if(r->state==1&&c.state==0){r->state=0;r->time=c.time;}}}}}*/
做法简介:暴力求解百分之百超时,应该先把输入按时间排序,建议用快速排序,然后建立哈希表,并且根据in和out的4种情况讨论,记录下当时的in时间和out时间(用一个24*60*60的数组记录,如果in那么那个时刻的值就+1,out就-1,最后学校里的车就是in和out量的“积分”,只需要一次遍历就可以了)停留最大值的记录也可以通过一次遍历得到。
感想:
1.做了我3个小时。。。。坚持不看别人的代码,但是最后通过后还是借鉴了别人的用STL的map代替哈希表的,果然最后快了很多
2.这道题有一点很重要的就是理解题意!!某一时刻进入的车也算这个时刻的!出去的不算!
3.如果用我上面的方法,那么【查询序列是否递增对运行时间没有影响!!】
4.果然STL就是吊,用自己写的哈希表比map慢了一倍。。。。有空好好研究源码
0 0
原创粉丝点击