Bootstrap

1095.Cars on Campus

【题意】
        给出一些车进出校园的记录,计算特定时间点停在学校里的车的数量,并输出停的时间最长的车的车牌号以及停车时长。
【思路】

        这题和1016有点像,都要匹配 in 和 off。先按照车牌号和时间排序,筛去错误的车辆进出记录并计算最长停车时长以及相应的车辆;再按照时间排序,对于每一条记录模拟车辆进出过程即可。


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

struct record{
	string pNumber;
	int tt;
	bool in;
};

bool cmpNumber(record r1, record r2){
	return r1.pNumber < r2.pNumber || (r1.pNumber == r2.pNumber && r1.tt < r2.tt);
}

bool cmpTime(record r1, record r2){
	return r1.tt < r2.tt;
}

int main(){
	int n, k;
	cin >> n >> k;
	vector<record> records(n);
	for (int i = 0; i < n; ++i){
		int hh, mm, ss;
		string tmp;
		cin >> records[i].pNumber;
		scanf("%d:%d:%d", &hh, &mm, &ss);
		cin >> tmp;
		records[i].tt = (hh * 60 + mm) * 60 + ss;
		records[i].in = tmp[0] == 'i';
	}
	sort(records.begin(), records.end(), cmpNumber);
	vector<record> validRecords;
	vector<string> resNumbers;
	int total = -1, longest = 0, last_in;
	string tmp;
	bool first = 1, in = 1;
	for (int i = 0; i < n; ++i){
		if (first || tmp != records[i].pNumber){
			first = 0;
			if (!in){
				validRecords.pop_back();
			}
			if (total > longest){
				resNumbers.clear();
				resNumbers.push_back(tmp);
				longest = total;
			}
			else if (total == longest){
				resNumbers.push_back(tmp);
			}
			tmp = records[i].pNumber;
			in = 1;
			total = 0;
		}
		if (in){
			if (records[i].in){
				validRecords.push_back(records[i]);
				in = 0;
				last_in = records[i].tt;
			}
		}
		else{
			if (records[i].in){
				validRecords.pop_back();
				validRecords.push_back(records[i]);
				last_in = records[i].tt;
			}
			else{
				validRecords.push_back(records[i]);
				total += records[i].tt - last_in;
				in = 1;
			}
		}
	}
	if (!in){
		validRecords.pop_back();
	}
	if (total > longest){
		resNumbers.clear();
		resNumbers.push_back(tmp);
		longest = total;
	}
	else if (total == longest){
		resNumbers.push_back(tmp);
	}

	sort(validRecords.begin(), validRecords.end(), cmpTime);
	int index = 0, cnt = 0;
	for (int i = 0; i < k; ++i){
		int hh, mm, ss, tt;
		scanf("%d:%d:%d", &hh, &mm, &ss);
		tt = (hh * 60 + mm) * 60 + ss;
		while (index < validRecords.size() && validRecords[index].tt <= tt){
			if (validRecords[index].in){
				++cnt;
			}
			else{
				--cnt;
			}
			++index;
		}
		cout << cnt << endl;
	}
	sort(resNumbers.begin(), resNumbers.end());
	for (auto i : resNumbers){
		cout << i << " ";
	}
	printf("%02d:%02d:%02d", longest / 3600, (longest / 60) % 60, longest % 60);

	system("pause");
	return 0;
}


悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;