上机训练实战指南-PAT Advance1095

题目链接
这是一个30分的题目, 不太容易, 考察的内容也很多,排序, map容器等,最后我也是看了别人的题解才理解了具体的做法,感觉最主要的还是英语问题,因为题目都是英文的,有些时候有的地方理解不到位,恰巧是题目中的关键点,所以还需要加强英语能力以及编码能力才行。

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
using namespace std;
struct node {
	char id[10];
	int time, flag = 0;
};
bool cmp1(node a, node b) {
	if(strcmp(a.id, b.id) != 0)
		return strcmp(a.id, b.id) < 0;
	else
		return a.time < b.time;
}
bool cmp2(node a, node b) {
	return a.time < b.time;
}
int main() {
	int n, k, maxtime = -1, tempindex = 0;
	scanf("%d%d\n", &n, &k);
	vector<node> record(n), car;
	for(int i = 0; i < n; i++) {
		char temp[5];
		int h, m, s;
		scanf("%s %d:%d:%d %s\n", record[i].id, &h, &m, &s, temp);
		int temptime = h * 3600 + m * 60 + s;
		record[i].time = temptime;
		record[i].flag = strcmp(temp, "in") == 0 ? 1 : -1;
	}
	sort(record.begin(), record.end(), cmp1);
	map<string, int> mapp;
	for(int i = 0; i < n - 1; i++) {
		if(strcmp(record[i].id, record[i+1].id) == 0 && record[i].flag == 1 &&
		        record[i+1].flag == -1) {
			car.push_back(record[i]);
			car.push_back(record[i+1]);
			mapp[record[i].id] += (record[i+1].time - record[i].time);
			if(maxtime < mapp[record[i].id]) {
				maxtime = mapp[record[i].id];
			}
		}
	}
	sort(car.begin(), car.end(), cmp2);
	vector<int> cnt(n);
	for(int i = 0; i < car.size(); i++) {
		if(i == 0)
			cnt[i] += car[i].flag;
		else
			cnt[i] = cnt[i - 1] + car[i].flag;
	}
	for(int i = 0; i < k; i++) {
		int h, m, s;
		scanf("%d:%d:%d", &h, &m, &s);
		int temptime = h * 3600 + m * 60 + s;
		int j;
		for(j = tempindex; j < car.size(); j++) {
			if(car[j].time > temptime) {
				printf("%d\n", cnt[j - 1]);
				break;
			} else if(j == car.size() - 1) {
				printf("%d\n", cnt[j]);
			}
		}
		tempindex = j;
	}
	for(map<string, int>::iterator it = mapp.begin(); it != mapp.end(); it++) {
		if(it->second == maxtime)
			printf("%s ", it->first.c_str());
	}
	printf("%02d:%02d:%02d", maxtime / 3600, (maxtime % 3600) / 60, maxtime %
	       60);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值