Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Advanced/C++/1017] Suggestion #134

Open
xwcai98 opened this issue Mar 8, 2020 · 0 comments
Open

[Advanced/C++/1017] Suggestion #134

xwcai98 opened this issue Mar 8, 2020 · 0 comments

Comments

@xwcai98
Copy link

xwcai98 commented Mar 8, 2020

虽然同样使用了优先队列记录结束时间,但是优先队列其实不需要初始化,我认为以下写法更加优秀。

#include <bits/stdc++.h>
using namespace std;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	const int st = 8 * 3600, en = 17 * 3600;
	int n, k;
	cin >> n >> k;
	vector<pair<int, int>> a;
	for (int i = 0; i < n; ++i) {
		string time;
		int op;
		cin >> time >> op;
		int t = stoi(time.substr(0, 2)) * 3600 + 
			stoi(time.substr(3, 2)) * 60 + stoi(time.substr(6, 2));
		if (t >= en) continue;
		a.emplace_back(t, min(op * 60, 3600));
	}
	sort(a.begin(), a.end());
	int tot = 0, now = st;
	priority_queue<int, vector<int>, greater<int>> q;
	for (auto e: a) {
		now = max(now, e.first);
		while (!q.empty() && q.top() <= now) q.pop();
		if (q.size() == k) {
			now = max(now, q.top());
			q.pop();
		}
		q.push(now + e.second);
		tot += now - e.first;
	}
	int cnt = a.size();
	cout << fixed << setprecision(1) << tot / 60.0 / cnt << '\n';
	return 0;
} 
@xwcai98 xwcai98 changed the title [Advanced/C++/1012] Suggestion [Advanced/C++/1017] Suggestion Mar 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant