本文主要是介绍PAT 甲级 1095 Cars on Campus,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
PAT 甲级 1095 Cars on Campus
#include <bits/stdc++.h>
using namespace std;
const int kMAXN = 10010;
struct Car
{char id[8];int time;char status[4];
} all[kMAXN], valid[kMAXN];
int valid_num = 0;
map<string, int> parkTime;
int TimeToInt(int hh, int mm, int ss)
{return hh * 3600 + mm * 60 + ss;
}bool CmpIdAndTime(const Car& a, const Car& b)
{if (strcmp(a.id, b.id))return strcmp(a.id, b.id) < 0;return a.time < b.time;
}bool CmpTime(const Car& a, const Car& b)
{return a.time < b.time;
}int main()
{
#ifdef LOCALfreopen("input.txt", "r", stdin);
#endifint n, k, hh, mm, ss;cin >> 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, CmpIdAndTime);int max_time = -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[valid_num++] = all[i];valid[valid_num++] = all[i + 1];int this_in_time = all[i + 1].time - all[i].time;parkTime[all[i].id] += this_in_time;max_time = max(max_time, parkTime[all[i].id]);}}sort(valid, valid + valid_num, CmpTime);int now = 0;int num_car = 0;for (int i = 0; i < k; ++i) {scanf("%d:%d:%d", &hh, &mm, &ss);int time = TimeToInt(hh, mm, ss);while (now < valid_num && valid[now].time <= time) {if (!strcmp(valid[now].status, "in"))num_car++;elsenum_car--;now++;}printf("%d\n", num_car);}for (auto itr = parkTime.begin(); itr != parkTime.end(); ++itr) {if (itr->second == max_time) {printf("%s ", itr->first.c_str());}}printf("%02d:%02d:%02d", max_time / 3600, max_time % 3600 / 60,max_time % 60);
}
这篇关于PAT 甲级 1095 Cars on Campus的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!