PAT练习(1)-1006 Cars on Campus

来源:互联网 发布:java关键字定义常量 编辑:程序博客网 时间:2024/06/16 09:51

题目来源

牛客网的PAT练习(https://www.nowcoder.com/pat/5/problem/4319):1006 Cars on Campus

题目描述

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.

输入描述:

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 formatplate_number hh:mm:ss statuswhere 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.


输出描述:

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.

输入例子:

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

输出例子:

1452101JH007BD ZD00001 07:20:09

思路

        题目需要求解的是在校园内停留的车辆的数量,以及总停留时间最长的汽车。
        先求每辆车的停留时间 应该直接将每辆车的出校时间减去入校时间就可以了。但是数据并不一定是完全对应的(每一个入校的in并不一定有对应的出校out),这就需要过滤掉不合理的记录。我们应该选择在时间上in和out相邻的记录作为有效记录(对应同一辆车),也就是一些列的in中只有最后一个有效,一系列out中只有第一个有效。
        在求解每个时间内校内车辆数目X 上一段中有效的in对应的时间点,X应该加一,out对应的时间点,X应该减一。构建一个
int carsInCampus[24*3600];
数组,初始值为0,in的时间点+1,out的时间点-1。然后再次遍历该数组,求出每个时间点的X:
carsInCampus[i] += carsInCampus[i-1];

答案代码

#include "stdio.h"#include <algorithm>#include <string>#include <iostream>#include <vector>#include <map>using namespace std;struct _car {string number = "A";int time_total = 0;int lastIn = -1;int lastOut = -1;};struct _record{string plate_number;int time = -1;bool isIn;};bool cmp(_record re1, _record re2){/*int c = strcmp(re1.plate_number.c_str(), re2.plate_number.c_str());*/int c = re1.plate_number.compare(re2.plate_number);if (c < 0)return true;if (c > 0)return false;return re1.time < re2.time;}vector<_record> actions;int carsInCampus[24 * 3600] = { 0 };map<string,_car> cars;int main(){int n, k;cin >> n >> k;string tmp_str;int tmp_h = 0, tmp_m = 0, tmp_s = 0;string tmp_in;for (int index = 0; index < n; index++){cin >> tmp_str;scanf("%d:%d:%d", &tmp_h, &tmp_m, &tmp_s);cin >> tmp_in;_record tmp_record;tmp_record.plate_number = tmp_str;tmp_record.time = tmp_h * 3600 + tmp_m * 60 + tmp_s;tmp_record.isIn = (tmp_in.compare("in") == 0);actions.push_back(tmp_record);}sort(actions.begin(), actions.end(), cmp);for (int i = 1; i < actions.size(); i++){_record re_pre = actions.at(i - 1);_record re = actions.at(i);if (re_pre.plate_number == re.plate_number &&re_pre.isIn &&!re.isIn){_car & car = cars[re_pre.plate_number];car.number = re_pre.plate_number;car.time_total += (re.time - re_pre.time);carsInCampus[re_pre.time]++;carsInCampus[re.time]--;}}for (int i = 1; i < 24 * 3600; i++){carsInCampus[i] += carsInCampus[i - 1];}vector<int> times;for (int i = 0; i < k; i++){scanf("%d:%d:%d", &tmp_h, &tmp_m, &tmp_s);times.push_back(tmp_h * 3600 + tmp_m * 60 + tmp_s);}for (int i = 0; i < times.size(); i++){cout << carsInCampus[times[i]] << endl;}// output carint maxPeriod = -1;vector<string> numbers;for (map<string, _car>::iterator it = cars.begin();it != cars.end(); it++){_car c = it->second;if (c.time_total > maxPeriod){maxPeriod = c.time_total;numbers.clear();numbers.push_back(it->first);}else if (c.time_total == maxPeriod){numbers.push_back(it->first);}}for (int i = 0; i < numbers.size(); i++){cout << numbers.at(i) << " ";}printf("%02d:%02d:%02d", maxPeriod / 3600, maxPeriod % 3600 / 60, maxPeriod % 60);return 0;}



原创粉丝点击