1095. Cars on Campus (30)
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:00Sample Output:
1 4 5 2 1 0 1 JH007BD ZD00001 07:20:09
输入
N个车辆进出记录 K个要查询的时间点
N行 车牌 时间(00:00:00~23:59:59) 进in/出out
……
K行 查询的时间(对应输出这个时间点停车场的车辆数。这个时间点如果正好有车有操作,那么车辆数是操作后的 【进+1/出-1/无0】)
……
最后给出一天中累计停放的时间最长的车牌号(多个按字母序输出) 并输出这个最长时间
PS:由于K行的查询是按时间顺序进行的,那么可以直接从头搜到尾。也可以用统计放到数组里面再用二分法,测试出来的时间差不多,内存有点区别。但是如果全部用cin,cout会偶尔提交AC偶尔超时。
下面代码,一:无二分法scanf-printf;
二:二分法scanf-prinft;
三:二分法cin-cout(第三个看一下时间,就是会偶尔超时的那个,太酸爽了)
评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月19日 15:35 | 答案正确 | 30 | 1095 | C++ (g++ 4.7.2) | 48 | 948 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 296 | 18/18 |
1 | 答案正确 | 11 | 948 | 3/3 |
2 | 答案正确 | 11 | 564 | 3/3 |
3 | 答案正确 | 1 | 384 | 1/1 |
4 | 答案正确 | 48 | 820 | 3/3 |
5 | 答案正确 | 10 | 820 | 2/2 |
#include<iostream> #include<string.h> #include<string> #include<vector> #include<algorithm> using namespace std; struct carslist { char plate[9]; //车牌号 int time; bool in; // in为true,out为false }; void readln(vector<carslist>*cars, int N) //最初输入数据。所有car数据都存在一起,后面再剔除。 { int hh, mm, ss; char in_out[5]; int index; for (index = 0; index < N; index++) { scanf("%s%d:%d:%d%s", (*cars)[index].plate, &hh, &mm, &ss,in_out); //cin >> (*cars)[index].plate >> hh >> in_out[0] >> mm >> in_out[0] >> ss >> in_out; (*cars)[index].time = hh * 3600 + mm * 60 + ss; (*cars)[index].in = in_out[0] == 'i' ? true : false; } } bool everycarsTimecmp(const carslist&A, const carslist&B) //根据车牌号排序。 { if (strcmp(A.plate,B.plate)==0)return A.time < B.time; else return strcmp(A.plate, B.plate)<0; } bool TotalTimesort(const carslist&A, const carslist&B) { return A.time < B.time; } int KickWrong_Addlongest(vector<carslist>*cars, vector<string>*staylongest) //剔除无用数据,以及找最大值 { int maxTime = 0,ThecarmaxT=0; string Precar=(*cars)[0].plate; for (vector<carslist>::iterator iter = (*cars).begin(); iter != (*cars).end(); ) //注意没有iter++ { if (iter->in&&iter + 1 != (*cars).end() && strcmp(iter->plate, (iter + 1)->plate)== 0 && !(iter + 1)->in) /*in_out一定成对存在,题目要求*/ { if (Precar == iter->plate) ThecarmaxT += (iter + 1)->time - iter->time;/*累记一整天的停车时长*/ else { Precar = iter->plate; ThecarmaxT = (iter + 1)->time - iter->time; } if (maxTime < ThecarmaxT) { (*staylongest).clear(); //专门开一个数组记录最长时间 (*staylongest).push_back(Precar); maxTime = ThecarmaxT; } else if (maxTime == ThecarmaxT) (*staylongest).push_back(Precar); iter += 2; //每次正确位置,直接+2 } else iter = (*cars).erase(iter); //重要!剔除无用数据 } return maxTime; } void read_ans(int K, vector<carslist>*cars) { int hh, mm, ss,firstindex=0,cnt=0; while (K--) { //cin >> hh >>maohao>> mm>>maohao >> ss; scanf("%d:%d:%d", &hh, &mm, &ss); hh=hh*3600+mm*60+ss; for (; firstindex< (*cars).size()&&(*cars)[firstindex].time<=hh;firstindex++) if ((*cars)[firstindex].in) //按时间排序后,进一辆车cnt++。出一辆cnt-- cnt++; else cnt--; printf("%d\n",cnt); } } int main() { int N, K; scanf("%d%d", &N, &K); vector<carslist>cars(N); readln(&cars, N); sort(cars.begin(), cars.end(), everycarsTimecmp); vector<string>staylongest; N=KickWrong_Addlongest(&cars,&staylongest); sort(cars.begin(), cars.end(), TotalTimesort); read_ans(K, &cars); for (vector<string>::iterator iter = staylongest.begin(); iter != staylongest.end(); iter++) printf("%s ", (*iter).c_str()); printf("%02d:%02d:%02d\n", N / 3600, N % 3600 / 60, N % 60); system("pause"); return 0; }评测结果
时间 | 结果 | 得分 | 题目 | 语言 | 用时(ms) | 内存(kB) | 用户 |
---|---|---|---|---|---|---|---|
8月19日 15:00 | 答案正确 | 30 | 1095 | C++ (g++ 4.7.2) | 50 | 1012 | datrilla |
测试点
测试点 | 结果 | 用时(ms) | 内存(kB) | 得分/满分 |
---|---|---|---|---|
0 | 答案正确 | 1 | 296 | 18/18 |
1 | 答案正确 | 11 | 1012 | 3/3 |
2 | 答案正确 | 11 | 692 | 3/3 |
3 | 答案正确 | 1 | 412 | 1/1 |
4 | 答案正确 | 50 | 948 | 3/3 |
5 | 答案正确 | 10 | 948 | 2/2 |
#include<iostream> #include<string.h> #include<string> #include<vector> #include<algorithm> using namespace std; struct carslist { char plate[9]; int time; bool in; }; void readln(vector<carslist>*cars, int N) { int hh, mm, ss; char in_out[5]; int index; for (index = 0; index < N; index++) { scanf("%s%d:%d:%d%s", (*cars)[index].plate, &hh, &mm, &ss,in_out); //cin >> (*cars)[index].plate >> hh >> in_out[0] >> mm >> in_out[0] >> ss >> in_out; (*cars)[index].time = hh * 3600 + mm * 60 + ss; (*cars)[index].in = in_out[0] == 'i' ? true : false; } } bool everycarsTimecmp(const carslist&A, const carslist&B) { if (strcmp(A.plate,B.plate)==0)return A.time < B.time; else return strcmp(A.plate, B.plate)<0; } bool TotalTimesort(const carslist&A, const carslist&B) { return A.time < B.time; } int KickWrong_Addlongest(vector<carslist>*cars, vector<string>*staylongest) { int maxTime = 0,ThecarmaxT=0; string Precar=(*cars)[0].plate; for (vector<carslist>::iterator iter = (*cars).begin(); iter != (*cars).end(); ) { if (iter->in&&iter + 1 != (*cars).end() && strcmp(iter->plate, (iter + 1)->plate)== 0 && !(iter + 1)->in) /*in_out一定成对存在,题目要求*/ { if (Precar == iter->plate) ThecarmaxT += (iter + 1)->time - iter->time;/*累记一整天的停车时长*/ else { Precar = iter->plate; ThecarmaxT = (iter + 1)->time - iter->time; } if (maxTime < ThecarmaxT) { (*staylongest).clear(); (*staylongest).push_back(Precar); maxTime = ThecarmaxT; } else if (maxTime == ThecarmaxT) (*staylongest).push_back(Precar); iter += 2; } else iter = (*cars).erase(iter); } return maxTime; } void numbercount(vector<int>*number, vector<carslist>*cars) //罗列出每个位置的cnt值,存进number里。方便hash { int index, len = (*cars).size(),cnt; for (cnt=index = 0; index < len; index++) { if ((*cars)[index].in)cnt++; else cnt--; (*number).push_back(cnt); } } int binaryFind_noMoretime(vector<carslist>*cars, int time,int firstindex) 二分确定截止位置,用上面数据直接hash { int lastindex = (*cars).size() - 1; if (lastindex == -1 || time<(*cars)[0].time)return -1; else if (time>(*cars)[lastindex].time) return lastindex; int midindex; while (firstindex <= lastindex) { midindex = (lastindex + firstindex) / 2; if ((*cars)[midindex].time > time)lastindex = midindex - 1; else firstindex = midindex + 1; } return lastindex; } void read_ans(int K, vector<carslist>*cars, vector<int>*number) { int hh, mm, ss,firstindex=0; while (K--) { //cin >> hh >>maohao>> mm>>maohao >> ss; scanf("%d:%d:%d", &hh, &mm, &ss); firstindex=binaryFind_noMoretime(cars, hh * 3600 + mm * 60 + ss,firstindex); if (firstindex == -1) printf("0\n"); else printf("%d\n", (*number)[firstindex]); } } int main() { int N, K; scanf("%d%d", &N, &K); vector<carslist>cars(N); readln(&cars, N); sort(cars.begin(), cars.end(), everycarsTimecmp); vector<string>staylongest; N=KickWrong_Addlongest(&cars,&staylongest); sort(cars.begin(), cars.end(), TotalTimesort); vector<int>number; numbercount(&number, &cars); read_ans(K, &cars, &number); for (vector<string>::iterator iter = staylongest.begin(); iter != staylongest.end(); iter++) printf("%s ", (*iter).c_str()); printf("%02d:%02d:%02d\n", N / 3600, N % 3600 / 60, N % 60); system("pause"); return 0; }