1095. Cars on Campus 解析

当前时刻停车厂有多少车,然后一天下来停的最久的车是哪几辆,时间是多久。

注意:

in、out是有不匹配的情况的。

还有题目里面说一辆车不会同时进出。我理解成几辆车不会同时进出了。结果发现样例都有同时进的。

匹配完,其实停车时间就出来了。如果车几次进出,最后求的是总的时间。

我直接开了个3601* 24的数组,每一秒来统计车辆的进出情况,然后从前往后累加,就是每个时刻的车辆数量。

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>

#define MAX 3601*24

using namespace std;

int n, k;

struct NodeCar {
	string id;
	int tag;
	int time;
};
vector <NodeCar> list;

struct NodeSeg {
	int time;
	string id;
	NodeSeg() { time = 0; }
};

NodeSeg ParkTime[MAX];
int Count[MAX];
set <string> CarId;
map <string, int> id2int;
map <int, string> int2id;


bool cmp(NodeCar n1, NodeCar n2) {
	if (n1.id != n2.id)
		return n1.id < n2.id;
	else 
		return n1.time < n2.time;
}

bool cmp2(NodeSeg s1, NodeSeg s2) {
	if (s1.time != s2.time)
		return s1.time > s2.time;
	else
		return s1.id < s2.id;
}

void TimePrint(int time) {
	int hh = time / 3600;
	int mm = time % 3600 / 60;
	int ss = time % 3600 % 60;
	printf("%02d:%02d:%02d\n", hh, mm, ss);
}

int main() {
	cin >> n >> k;
	string s, tag;
	int hh, mm, ss, t;
	memset(Count, 0, sizeof(Count));
	NodeSeg tempSeg;

	NodeCar  tempCar;
	for (int i = 0; i < n; i++) {
		cin >> tempCar.id;
		CarId.insert(tempCar.id);

		cin >> hh;
		cin.get();
		cin >> mm;
		cin.get();
		cin >> ss >> tag;
		t = 3600 * hh + 60 * mm + ss;
		tempCar.time = t;
		if (tag == "in")
			tempCar.tag = 1;
		else if (tag == "out")
			tempCar.tag = 2;
		list.push_back(tempCar);
	}

	sort(list.begin(), list.end(), cmp);

	set <string>::iterator it;
	int num = 0;
	for (it = CarId.begin(); it != CarId.end(); it++) {
		id2int[*it] = num;
		int2id[num++] = *it;
	}

	//In、out匹配
	bool isIn = false;
	int pre, p;
	for (int i = 0; i < list.size(); i++) {
		if (!isIn  && list[i].tag == 1) {
			isIn = true;
			pre = i;
		}
		else if (isIn && list[i].tag == 1) {
			pre = i;
		}
		else if (isIn && list[i].tag == 2 && list[i].id == list[pre].id) {
			Count[list[pre].time]++;
			Count[list[i].time]--;
			ParkTime[id2int[list[i].id]].time += (list[i].time - list[pre].time);
			ParkTime[id2int[list[i].id]].id = list[pre].id;
			isIn = false;
		}
	}

	int sum = 0;
	for (int i = 0; i < MAX; i++) {
		sum += Count[i];
		Count[i] = sum;
	}

	sort(ParkTime, ParkTime + CarId.size(), cmp2);


	for (int i = 0; i < k; i++) {
		cin >> hh;
		cin.get();
		cin >> mm;
		cin.get();
		cin >> ss;
		t = 3600 * hh + 60 * mm + ss;
		cout << Count[t] << endl;
	}

	int max = ParkTime[0].time;

	for (int i = 0; i < CarId.size(); i++) {
		if (ParkTime[i].time == max) {
			cout << ParkTime[i].id << " ";
		}
	}
	TimePrint(max);
	

	return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值