您的位置:首页 > 其它

1095

2015-08-17 20:29 337 查看


1095. Cars on Campus (30)

时间限制

220 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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:0
4000
0

Sample Output:
1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09

考PAT时,因为这个题目而丢了三分,再刷的时候已不记得之前的思维了,而以下的代码思路显得更清晰了。

#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

struct Car
{
string plate;
int stay;
vector<int> record;
Car() : stay(0) { }
bool operator < (const Car &c) const
{
if (stay != c.stay)
return stay > c.stay;
return plate < c.plate;
}
};
const int N = 10000;
Car car
;
map<string, int> mp;
struct Record
{
int plate;
int time;
bool status;//记录是进还是出
bool flag;//记录数据是否有效
Record() : flag(false){ }
bool operator < (const Record &re) const
{
return time < re.time;
}
};
vector<Record> rec;//将所有记录放到rec数组中,以时间顺序排序

int main(void)
{
int n, k;
scanf("%d%d", &n, &k);
string plate;
int hh, mm, ss, index = 0, carIndex;
char status[4];
Record record;
for (int i = 0; i < n; ++i)
{
cin >> plate;
scanf("%d:%d:%d%s", &hh, &mm, &ss, status);
//首先判断是否做映射
if (mp.find(plate) == mp.end())
{
mp.insert(make_pair(plate, index));
carIndex = index;
car[carIndex].plate = plate;
++index;
}
else
carIndex = mp[plate];
record.plate = carIndex;
record.time = hh * 60 * 60 + mm * 60 + ss;
if (status[0] == 'i')
record.status = true;
else
record.status = false;
rec.push_back(record);
}
sort(rec.begin(), rec.end());
//将每个记录归到每个车的结构体中,判断记录是否有效
for (int i = 0; i < n; ++i)
car[rec[i].plate].record.push_back(i);

for (int i = 0; i < index; ++i)
{
int j = 0, length = car[i].record.size();
while (j < length - 1)
{
if (rec[car[i].record[j]].status == true && rec[car[i].record[j+1]].status == false)
{
rec[car[i].record[j]].flag = true;
rec[car[i].record[j+1]].flag = true;
car[i].stay += rec[car[i].record[j+1]].time - rec[car[i].record[j]].time;
j = j + 2;
}
else
++j;
}
}
int j = 0, nums = 0;
for (int i = 0; i < k; ++i)
{
scanf("%d:%d:%d", &hh, &mm, &ss);
int time = hh * 60 * 60 + mm * 60 + ss;
while (j < n && rec[j].time <= time)
{
if (rec[j].flag == true)
{
if (rec[j].status == true)
++nums;
else
--nums;
}
++j;
}
printf("%d\n", nums);
}
sort (car, car+n);
cout << car[0].plate;
for (int i = 1; i < n; ++i)
{
if (car[i].stay == car[i-1].stay)
cout << " " << car[i].plate;
else
break;
}
printf(" %02d:%02d:%02d\n", car[0].stay / 3600, (car[0].stay / 60) % 60, car[0].stay % 60);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  排序