Submission #981905

# Submission time Handle Problem Language Result Execution time Memory
981905 2024-05-13T16:15:43 Z michified Palembang Bridges (APIO15_bridge) C++17
100 / 100
61 ms 4612 KB
#include <bits/stdc++.h>
#define ll long long
using namespace std;

ll ltot, rtot;
priority_queue<int> pql;
priority_queue<int, vector<int>, greater<int>> pqr;

void put(ll x) {
	ll med = pql.empty() ? LLONG_MAX : pql.top(), tmp;
	if (x <= med) {
		pql.push(x);
		ltot += x;
	} else {
		pqr.push(x);
		rtot += x;
	}
	if (pqr.size() + 1 < pql.size()) {
		tmp = pql.top();
		pql.pop();
		pqr.push(tmp);
		ltot -= tmp;
		rtot += tmp;
	} else if (pqr.size() > pql.size()) {
		tmp = pqr.top();
		pqr.pop();
		pql.push(tmp);
		ltot += tmp;
		rtot -= tmp;
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int k, n, i;
	cin >> k >> n;
	char az, bz;
	ll ap, bp, ss = 0, best = 0;
	vector<ll> pos;
	vector<pair<ll, ll>> ppl;
	for (i = 0; i < n; i++) {
		cin >> az >> ap >> bz >> bp;
		if (az == bz) ss += abs(ap - bp);
		else {
			if (ap > bp) swap(ap, bp);
			ppl.push_back({ap, bp});
			ss++;
		}
	}
	n = ppl.size();
	sort(ppl.begin(), ppl.end(), [](const pair<int, int>& one, const pair<int, int>& two){return one.first + one.second < two.first + two.second;});
	ltot = rtot = 0;
	vector<ll> pref(n + 1);
	for (i = 0; i < n; i++) {
		put(ppl[i].first);
		put(ppl[i].second);
		pref[i + 1] = rtot - ltot;
	}
	best = pref[n];

	if (k == 2) {
		while (not pql.empty()) pql.pop();
		while (not pqr.empty()) pqr.pop();
		ltot = rtot = 0;
		for (i = n - 1; i >= 0; i--) {
			put(ppl[i].first);
			put(ppl[i].second);
			best = min(best, rtot - ltot + pref[i]);
		}
	}
	cout << ss + best;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 600 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 500 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 17 ms 3960 KB Output is correct
13 Correct 35 ms 4044 KB Output is correct
14 Correct 27 ms 3796 KB Output is correct
15 Correct 24 ms 2772 KB Output is correct
16 Correct 18 ms 4052 KB Output is correct
17 Correct 30 ms 4044 KB Output is correct
18 Correct 19 ms 4308 KB Output is correct
19 Correct 34 ms 3880 KB Output is correct
20 Correct 25 ms 4300 KB Output is correct
21 Correct 33 ms 4052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 25 ms 4052 KB Output is correct
26 Correct 44 ms 4308 KB Output is correct
27 Correct 59 ms 4296 KB Output is correct
28 Correct 61 ms 4296 KB Output is correct
29 Correct 61 ms 4552 KB Output is correct
30 Correct 31 ms 2704 KB Output is correct
31 Correct 22 ms 4052 KB Output is correct
32 Correct 49 ms 4052 KB Output is correct
33 Correct 35 ms 4308 KB Output is correct
34 Correct 55 ms 3952 KB Output is correct
35 Correct 35 ms 4612 KB Output is correct
36 Correct 54 ms 3832 KB Output is correct
37 Correct 21 ms 4052 KB Output is correct