1095. Cars on Campus 解析

来源:互联网 发布:阿里云独立ip主机 编辑:程序博客网 时间:2024/06/01 21:25

当前时刻停车厂有多少车,然后一天下来停的最久的车是哪几辆,时间是多久。

注意:

in、out是有不匹配的情况的。

还有题目里面说一辆车不会同时进出。我理解成几辆车不会同时进出了。结果发现样例都有同时进的。

匹配完,其实停车时间就出来了。如果车几次进出,最后求的是总的时间。

我直接开了个3601* 24的数组,每一秒来统计车辆的进出情况,然后从前往后累加,就是每个时刻的车辆数量。

#include <iostream>#include <vector>#include <string>#include <cstring>#include <algorithm>#include <set>#include <map>#define MAX 3601*24using namespace std;int n, k;struct NodeCar {string id;int tag;int time;};vector <NodeCar> list;struct NodeSeg {int time;string id;NodeSeg() { time = 0; }};NodeSeg ParkTime[MAX];int Count[MAX];set <string> CarId;map <string, int> id2int;map <int, string> int2id;bool cmp(NodeCar n1, NodeCar n2) {if (n1.id != n2.id)return n1.id < n2.id;else return n1.time < n2.time;}bool cmp2(NodeSeg s1, NodeSeg s2) {if (s1.time != s2.time)return s1.time > s2.time;elsereturn s1.id < s2.id;}void TimePrint(int time) {int hh = time / 3600;int mm = time % 3600 / 60;int ss = time % 3600 % 60;printf("%02d:%02d:%02d\n", hh, mm, ss);}int main() {cin >> n >> k;string s, tag;int hh, mm, ss, t;memset(Count, 0, sizeof(Count));NodeSeg tempSeg;NodeCar  tempCar;for (int i = 0; i < n; i++) {cin >> tempCar.id;CarId.insert(tempCar.id);cin >> hh;cin.get();cin >> mm;cin.get();cin >> ss >> tag;t = 3600 * hh + 60 * mm + ss;tempCar.time = t;if (tag == "in")tempCar.tag = 1;else if (tag == "out")tempCar.tag = 2;list.push_back(tempCar);}sort(list.begin(), list.end(), cmp);set <string>::iterator it;int num = 0;for (it = CarId.begin(); it != CarId.end(); it++) {id2int[*it] = num;int2id[num++] = *it;}//In、out匹配bool isIn = false;int pre, p;for (int i = 0; i < list.size(); i++) {if (!isIn  && list[i].tag == 1) {isIn = true;pre = i;}else if (isIn && list[i].tag == 1) {pre = i;}else if (isIn && list[i].tag == 2 && list[i].id == list[pre].id) {Count[list[pre].time]++;Count[list[i].time]--;ParkTime[id2int[list[i].id]].time += (list[i].time - list[pre].time);ParkTime[id2int[list[i].id]].id = list[pre].id;isIn = false;}}int sum = 0;for (int i = 0; i < MAX; i++) {sum += Count[i];Count[i] = sum;}sort(ParkTime, ParkTime + CarId.size(), cmp2);for (int i = 0; i < k; i++) {cin >> hh;cin.get();cin >> mm;cin.get();cin >> ss;t = 3600 * hh + 60 * mm + ss;cout << Count[t] << endl;}int max = ParkTime[0].time;for (int i = 0; i < CarId.size(); i++) {if (ParkTime[i].time == max) {cout << ParkTime[i].id << " ";}}TimePrint(max);return 0;}


0 0
原创粉丝点击