A1095 Cars on Campus (30)(30 分)
A1095 Cars on Campus (30)(30 分)
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:00
Sample Output:
1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09
思考
《算法笔记》胡凡6.4节
map的常见用法






8月18日,还是买了纸质版,作为一份积累代码的纸质资料,还是很有用的,可以做到快速地代码复用。
c语言的string与c++的string
例(1)
函数声明:const char *c_str();
c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.
这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针
比如:最好不要这样:
char* c;
string s="1234";
c = s.c_str();
//c最后指向的内容是垃圾,因为s对象被析构,其内容被处理(纠正:s对象的析构是在对指针c完成赋值操作之后进行的,故此处并没有错误)
在vc++2010中提示的错误原因:
vc++2010中提示的错误原因
char c[20];
string s="1234";
strcpy(c,s.c_str());
这样才不会出错,c_str()返回的是一个临时指针,不能对其进行操作
c_str()返回的是一个分配给const char的地址,其内容已设定为不可变更,如果再把此地址赋给一个可以变更内容的char变量,就会产生冲突,在2010中是不被允许的。但是如果放入函数调用,或者直接输出,因为这些函数和输出都是把字符串指针作为 const char*引用的,所以不会有问题。
例(2)
c_str() 以const char* 类型返回 string 内含的字符串
如果一个函数要求char*参数,可以使用c_str()方法:
string s = "Hello World!";
printf("%s", s.c_str()); //输出 "Hello World!"
c_str在打开文件时的用处:
当需要打开一个由用户自己输入文件名的文件时,可以这样写:ifstream in(st.c_str());。其中st是string类型,存放的即为用户输入的文件名。
AC代码
/*由于要使用map,故在本体只有用c++来处理问题,map<string,int>
因此需要cstring string map,那么也使用<algorithm>的sort好了*/
#include <cstdio>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int maxn = 10010; //记录数N<=10000
struct Car{
char id[8];//7个英文大写字母,1位数字
int time;//将时间单位转化为s,这与前面的A1016计算话费的代码实现不同,那个用了while循环
char status[4];//in或者是out
}all[maxn],valid[maxn];//all为所有记录,valid为有效记录
int num = 0;//有效记录的条数
map<string, int> parkTime;//车牌号->总停留时间 ,这里的string要是c++的string类型才行
//timeToInt将时间转换为以s为单位,简化时间排序代码
int timeToInt(int hh, int mm, int ss){
return hh*3600 + mm * 60 + ss;
}
//将车牌号按字典序从小到大排序,若车牌号相同,则按时间从小到大排序
bool cmpByIdAndTime(Car a,Car b){
if(strcmp(a.id,b.id))
return strcmp(a.id,b.id) < 0;/*id升序*/
else return a.time < b.time;/*时间值升序*/
}
//按时间从小到大排序
bool cmpByTime(Car a, Car b){
return a.time < b.time;
}
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);//转换为以s为单位,简化了时间存储和比较的代码
}
sort(all,all+n,cmpByIdAndTime);//排序
int maxTime =-1;//最长总停留时间,哨兵
for(int i=0;i<n-1;i++){//遍历所有记录
if(!strcmp(all[i].id,all[i+1].id) && //i和i+1同一车牌号
!strcmp(all[i].status,"in") && //i是in记录
!strcmp(all[i+1].status,"out") ){//i+1是out记录
valid[num++]=all[i];//i和i+1配对,存入valid数组
valid[num++]=all[i+1];
int inTime = all[i+1].time - all[i].time; //此次停留时间
if(parkTime.count(all[i].id) == 0) {//count()的用法:返回关键字为all[i].id的数量,即车牌号为all[i].id的数量
parkTime[all[i].id]=0;//map中没有这个车牌号,置零 ,其实是节约成本的初始化
}
parkTime[all[i].id]+=inTime;//增加该车牌号的总停留时间
maxTime=max(maxTime,parkTime[all[i].id]);//更新最大总停留时间
}
}
sort(valid,valid+num,cmpByTime);//有效记录时间升序
//now指向不超过当前查询时间的记录,numCar为当前校园内车辆数
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);
//让now处理至当前查询时间
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=parkTime.begin();it != parkTime.end();it++){
if(it->second==maxTime){//输出所有最长总停留时间的车牌号
printf("%s ",it->first.c_str()); //c++字符串转为c语言字符串操作
}
}
printf("%02d:%02d:%02d\n",maxTime/3600,maxTime%3600/60,maxTime%60);
return 0;
}
A1095 Cars on Campus (30)(30 分)的更多相关文章
- 【刷题-PAT】A1095 Cars on Campus (30 分)
1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...
- PAT A1095 Cars on Campus (30 分)——排序,时序,从头遍历会超时
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- A1095 Cars on Campus (30 分)
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- 1095 Cars on Campus(30 分
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- A1095. Cars on Campus
Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...
- PAT甲级——A1095 Cars on Campus
Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out time ...
- PAT_A1095#Cars on Campus
Source: PAT A1095 Cars on Campus (30 分) Description: Zhejiang University has 8 campuses and a lot of ...
- PAT 1095 Cars on Campus
1095 Cars on Campus (30 分) Zhejiang University has 8 campuses and a lot of gates. From each gate we ...
- pat 甲级 Cars on Campus (30)
Cars on Campus (30) 时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard 题目描述 Zhejiang University ...
随机推荐
- MySQL分库分表的技巧
分表是分散数据库压力的好方法. 分表,最直白的意思,就是将一个表结构分为多个表,然后,可以再同一个库里,也可以放到不同的库. 当然,首先要知道什么情况下,才需要分表.个人觉得单表记录条数达到百万到千万 ...
- Backbone源码解析系列
01 编码风格.继承 02 Backbone.Events 03 Backbone.Model 04 Backbone.View 05 Backbone.Router 06 Backbone应用于we ...
- maven validator数据校验
1.maven文件中添加依赖包 <!-- validator校验--> <dependency> <groupId>org.hibernate</groupI ...
- ps钢笔工具路径问题
问题描述:ps钢笔工具画出路径后用文字工具打字 路径出现一个空心圆点字,不能在路径上打字或者无法确认终止的位置. 解决:1.如果要在路径上全都打满字,要将文字对齐改为左对齐,2.如果要实现自定义结束位 ...
- <Android 基础(四)> RecyclerView
介绍 RecyclerView是ListView的豪华增强版.它主要包含以下几处新的特性,如ViewHolder,ItemDecorator,LayoutManager,SmothScroller以及 ...
- Python开发环境Wing IDE如何调试进程异常报告
Wing IDE的调试器所报告的任何异常,都会在调试器以外的任何代码运行事件中展示出来. 通过使用Debug工具或者是Debug菜单中的Start / Continue继续调试过程的异常检测. Win ...
- c++ STL deque容器成员函数
deque是双向队列,即可以在头部插入删除,也可以在尾部插入删除.内部并不连续,这一点和vector并不一样.可能第1个元素和第2个元素的地址是不连在一起的.在使用时用it迭代器会安全一点. 这是c+ ...
- Coursera 算法二 week 4 Boggle
这次的作业主要用到了单词查找树和深度优先搜索. 1.在深度优先搜索中,在当前层的递归调用前,将marked数组标记为true.当递归调用返回到当前层时,应将marked数组标记为false.这样既可以 ...
- SAP成都研究院马洪波:提升学习力,增强竞争力,收获一生乐趣
马洪波是SAP成都研究院CEC开发团队三大巨头之一.关于他的背景介绍,参考我以前的公众号文章:SAP成都研究院CEC团队三巨头之一:M君的文章预告. 其实早在2007年,互联网上已经有介绍马洪波的文章 ...
- TFS看板晨会
迭代任务看板 打开任务看板 打开燃尽图查看剩余工作情况,如果离发布较近,但是还有很多剩余工作,可能需要提前准备移除一部分优先级低的需求,如果剩余工作较少,适当安排一些需求 任务板按照人员分组,查看每个 ...