题目关键:给时间算出该时间在校内车辆 最后一行写出停留时间最长的车子的名字和和停留时间 关键点:map容器使用,根据第一个元素排序 还有这个Note: the queries are given in ascending order of the times. 查询时间从小到大我草,顺着查一次就行了,不然有三个会超时,坑点真的太多了 ——————————————————————————————————————————————————
#include<iostream> #include<algorithm> #include<cstring> #include<string> #include<map> #include<cstdio> const int maxn = 10010; using namespace std; struct car { char id[15]; int time; char status[4]; }all[maxn], valid[maxn]; int num = 0; map<string, int>_time; bool cmp1(car a, car b) { if (strcmp(a.id, b.id)) return strcmp(a.id, b.id) < 0; else return a.time < b.time; } bool cmp2(car a, car b) { return a.time < b.time; } int timetoint(int hh, int mm, int ss) { return hh * 3600 + mm * 60 + ss; } int main() { int n, k, hh, mm, ss; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) { scanf("%s %d:%d:%d %s", all[i].id, &hh, &mm, &ss, all[i].status); all[i].time = timetoint(hh, mm, ss); } sort(all, all + n, cmp1); int maxtime = -1; for (int i = 0; i < n - 1; i++) { if (!strcmp(all[i].id, all[i + 1].id) && !strcmp(all[i].status, "in") && !strcmp(all[i + 1].status, "out")) { valid[num++] = all[i]; valid[num++] = all[i + 1]; int intime = all[i + 1].time - all[i].time; if (_time.count(all[i].id) == 0) { _time[all[i].id] = 0; } _time[all[i].id] += intime; maxtime = max(maxtime, _time[all[i].id]); } } //开始对有效数组进行操作了 sort(valid, valid + num, cmp2); int now=0,numcar = 0; for (int i = 0; i < k; i++) { scanf("%d:%d:%d", &hh, &mm, &ss); int time = timetoint(hh, mm, ss); while (now < num && valid[now].time <= time) { if (!strcmp(valid[now].status, "in")) numcar++; else numcar--; now++; } printf("%d\n",numcar); } map<string, int>::iterator it; for (it = _time.begin(); it != _time.end(); it++) { if (it->second == maxtime) { printf("%s ", it->first.c_str()); } } printf("%02d:%02d:%02d\n", maxtime / 3600, maxtime % 3600 / 60, maxtime % 60); return 0; }