Submission #871292

# Submission time Handle Problem Language Result Execution time Memory
871292 2023-11-10T13:35:31 Z qthang2k11 Palembang Bridges (APIO15_bridge) C++17
100 / 100
85 ms 6496 KB
// [+] dinhmanhhungwillwinioi2024
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int N = 1e5 + 5;

int k, n;
ll ans = 0;

vector<pair<int, int>> arr;
ll pref[N], suf[N];

priority_queue<int> p1;
priority_queue<int, vector<int>, greater<int>> p2;
ll s1, s2;

void add(int x) {
	if (!p1.empty() && x < p1.top()) {
		p1.emplace(x);
		s1 += x;
	} else {
		p2.emplace(x);
		s2 += x;
	}
	
	while (p1.size() > p2.size()) {
		int val = p1.top();
		
		p1.pop();
		s1 -= val;
		
		p2.emplace(val);
		s2 += val;
	}
	
	while (p2.size() > p1.size()) {
		int val = p2.top();
		
		p2.pop();
		s2 -= val;
		
		p1.emplace(val);
		s1 += val;
	}
}

inline ll cost() {
	int med = p1.top();
	return (1LL * med * p1.size() - s1) + (s2 - 1LL * med * p2.size());
}

void calc(ll dp[], bool rev) {
	while (!p1.empty()) p1.pop();
	while (!p2.empty()) p2.pop();
	s1 = s2 = 0;
	
	int m = arr.size();
	
	for (int i = 1; i <= m; i++) {
		add(arr[i - 1].first);
		add(arr[i - 1].second);
		dp[i] = cost();
	}
	
	if (rev) {
		reverse(dp + 1, dp + m + 1);
	}
}

int32_t main() {
	cin.tie(0)->sync_with_stdio(0);
	
	cin >> k >> n;
	for (int i = 1; i <= n; i++) {
		char c1, c2;
		int p1, p2;
		cin >> c1 >> p1 >> c2 >> p2;
		if (c1 == c2) {
			ans += abs(p1 - p2);
		} else {
			arr.emplace_back(p1, p2);
		}
	}
	
	sort(arr.begin(), arr.end(), [&] (const pair<int, int> &x, const pair<int, int> &y) {
		return x.first + x.second < y.first + y.second;
	});
	
	calc(pref, false);
	reverse(arr.begin(), arr.end());
	calc(suf, true);
	
	if (k == 1) {
		ans += pref[arr.size()];
	} else {
		int m = arr.size();
		ll res = ans + pref[m];
		for (int i = 1; i < m; i++)
			res = min(res, ans + pref[i] + suf[i + 1]);
		ans = res;
	}
	
	cout << ans + arr.size();
	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 500 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 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 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 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 496 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 476 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 30 ms 4660 KB Output is correct
13 Correct 85 ms 6356 KB Output is correct
14 Correct 71 ms 5072 KB Output is correct
15 Correct 48 ms 3984 KB Output is correct
16 Correct 34 ms 5696 KB Output is correct
17 Correct 67 ms 6168 KB Output is correct
18 Correct 69 ms 6092 KB Output is correct
19 Correct 71 ms 6348 KB Output is correct
20 Correct 40 ms 5904 KB Output is correct
21 Correct 72 ms 6192 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 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 464 KB Output is correct
8 Correct 0 ms 464 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 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 456 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 500 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 460 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 460 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 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 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 600 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 348 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 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 1 ms 344 KB Output is correct
8 Correct 0 ms 344 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 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 0 ms 348 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 1 ms 432 KB Output is correct
19 Correct 1 ms 476 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 348 KB Output is correct
25 Correct 31 ms 4696 KB Output is correct
26 Correct 68 ms 5580 KB Output is correct
27 Correct 83 ms 6036 KB Output is correct
28 Correct 85 ms 6496 KB Output is correct
29 Correct 85 ms 6176 KB Output is correct
30 Correct 44 ms 3540 KB Output is correct
31 Correct 34 ms 6096 KB Output is correct
32 Correct 67 ms 6372 KB Output is correct
33 Correct 69 ms 6348 KB Output is correct
34 Correct 72 ms 6240 KB Output is correct
35 Correct 40 ms 5840 KB Output is correct
36 Correct 77 ms 6060 KB Output is correct
37 Correct 25 ms 4816 KB Output is correct