Submission #925320

# Submission time Handle Problem Language Result Execution time Memory
925320 2024-02-11T12:02:17 Z belgianbot Palembang Bridges (APIO15_bridge) C++14
100 / 100
69 ms 10196 KB
#include <bits/stdc++.h>
#define int long long
#define sz(x) (long long)(x.size())

using namespace std;

struct traj{
	int l, r;
	long double mid;
};
vector <traj> a;

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int K, N; cin >> K >> N;
	
	int ans(0);
	for (int i(0); i < N; i++) {
		char riv1, riv2;
		int pos1, pos2;
		cin >> riv1 >> pos1 >> riv2 >> pos2;
		
		if (pos2 < pos1) swap(pos1, pos2);
		if (riv1 != riv2) {
			ans++;
			a.push_back ({pos1, pos2, (long double)( (pos1 + pos2) / 2)});
		}
		else ans += pos2 - pos1;
	}
	if (sz(a) == 0){
		 cout << ans << '\n';
		 return 0;
	 }
	sort (a.begin(), a.end(), [](traj &A, traj &B){return A.mid < B.mid;});
	
	priority_queue <int> left;
	priority_queue <int, vector<int>, greater <int>> right;
	
	vector <int> score (sz(a)), score2 (sz(a));
	
	int median = a[0].l;
	left.push(a[0].l);
	right.push(a[0].r);
	score[0] = a[0].r - a[0].l;
	for (int i(1); i < sz(a); i++) {
		score[i] = score[i - 1] + abs(a[i].l - median) + abs(a[i].r - median);
		if (a[i].l > left.top()) right.push(a[i].l);
		else left.push(a[i].l);
		if (a[i].r <=  left.top()) left.push(a[i].r);
		else right.push(a[i].r);
		int szL = sz(left), szR = sz(right);
		while (left.size() > right.size()) {
			right.push(left.top());
			left.pop();
		}
		while (left.size() < right.size()) {
			left.push(right.top());
			right.pop();
		}
		int newmedian = left.top();
		 
		if (newmedian > median) {
			score[i] += szL * (newmedian - median);
			score[i] -= szR * (newmedian - median);
		}
		else {
			score[i] -= (szL -1) * (median - newmedian);
			score[i] += (szR + 1) * (median - newmedian);
		}
		median = newmedian;
	}
	
	if (K == 1) {
		cout << ans + score[sz(a) - 1] << '\n';
		return 0;
	}
	while (!left.empty()) left.pop();
	while(!right.empty()) right.pop();
	
	median = a[sz(a) - 1].l;
	left.push(a[sz(a) - 1].l);
	right.push(a[sz(a) - 1].r);
	score2[sz(a) - 1] = (a[sz(a) - 1].r - median);
	int best = min(score[sz(a) - 1], score[sz(a) - 2] + score2[sz(a) - 1]);
	for (int i(sz(a) - 2); i > 0; i--) {
		score2[i] = score2[i + 1] + abs(a[i].l - median) + abs(a[i].r - median);
		if (a[i].l > left.top()) right.push(a[i].l);
		else left.push(a[i].l);
		if (a[i].r <= left.top()) left.push(a[i].r);
		else right.push(a[i].r);
		int szL = sz(left), szR = sz(right);
		while (left.size() > right.size()) {
			right.push(left.top());
			left.pop();
		}
		while (left.size() < right.size()) {
			left.push(right.top());
			right.pop();
		}
		int newmedian = left.top();
		
		if (newmedian > median) {
			score2[i] += szL * (newmedian - median);
			score2[i] -= szR * (newmedian - median);
		}
		else {
			score2[i] -= (szL - 1) * (median - newmedian);
			score2[i] += (szR + 1) * (median - newmedian);
		}
		median = newmedian;
		best = min(best, score[i - 1] + score2[i]);
	}
	cout << best + ans << '\n';
	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 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 344 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 600 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
12 Correct 23 ms 8912 KB Output is correct
13 Correct 43 ms 8896 KB Output is correct
14 Correct 33 ms 7876 KB Output is correct
15 Correct 25 ms 5312 KB Output is correct
16 Correct 24 ms 8384 KB Output is correct
17 Correct 34 ms 8624 KB Output is correct
18 Correct 27 ms 8664 KB Output is correct
19 Correct 42 ms 8572 KB Output is correct
20 Correct 31 ms 8108 KB Output is correct
21 Correct 40 ms 9164 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 344 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 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 1 ms 344 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 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 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 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 600 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 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
# 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 1 ms 344 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 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 356 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 352 KB Output is correct
18 Correct 1 ms 360 KB Output is correct
19 Correct 1 ms 496 KB Output is correct
20 Correct 1 ms 360 KB Output is correct
21 Correct 1 ms 360 KB Output is correct
22 Correct 1 ms 356 KB Output is correct
23 Correct 1 ms 360 KB Output is correct
24 Correct 1 ms 360 KB Output is correct
25 Correct 31 ms 8952 KB Output is correct
26 Correct 51 ms 8040 KB Output is correct
27 Correct 66 ms 8600 KB Output is correct
28 Correct 69 ms 10196 KB Output is correct
29 Correct 68 ms 8400 KB Output is correct
30 Correct 35 ms 4824 KB Output is correct
31 Correct 28 ms 8636 KB Output is correct
32 Correct 54 ms 8644 KB Output is correct
33 Correct 41 ms 8648 KB Output is correct
34 Correct 63 ms 9732 KB Output is correct
35 Correct 41 ms 8136 KB Output is correct
36 Correct 63 ms 8384 KB Output is correct
37 Correct 27 ms 8132 KB Output is correct