Submission #414819

# Submission time Handle Problem Language Result Execution time Memory
414819 2021-05-31T08:33:09 Z ngpin04 Palembang Bridges (APIO15_bridge) C++14
100 / 100
115 ms 6008 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 1e5 + 5; 

long long pre[N];
long long suf[N];
long long lsum, rsum;

pair <int, int> a[N];

int n,m,k;

priority_queue <int> lheap, rheap;

void add(int x) {
	int med = (lheap.size() ? lheap.top() : 1e9 + 1);
	if (x <= med) {
		lheap.push(x);
		lsum += x;
		if (lheap.size() > rheap.size() + 1) {
			x = lheap.top();
			lsum -= x;
			rsum += x;
			lheap.pop();
			rheap.push(-x);
		}
	} else {	
		rheap.push(-x);
		rsum += x;
		if (lheap.size() < rheap.size()) {
			x = -rheap.top();
			rsum -= x;
			lsum += x;
			rheap.pop();
			lheap.push(x);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> k >> n;
	long long same_side = 0;
	for (int i = 1; i <= n; i++) {	
		char cx, cy;
		int x, y;
		cin >> cx >> x >> cy >> y;
		if (cx == cy)
			same_side += abs(x - y);
		else {
			if (x > y)
				swap(x, y);
			a[++m] = mp(x, y);
		}
	}	

	sort(a + 1, a + m + 1, [](pair <int, int> a, pair <int, int> b) {
		return (a.fi + a.se < b.fi + b.se);
	});
	
	for (int i = 1; i <= m; i++) {
		add(a[i].fi);
		add(a[i].se);
		pre[i] = rsum - lsum;
	}

	lsum = rsum = 0;
	while (lheap.size())
		lheap.pop();
	while (rheap.size())
		rheap.pop();

	for (int i = m; i >= 1; i--) {
		add(a[i].fi);
		add(a[i].se);
		suf[i] = rsum - lsum;
	}

	long long res = 1e18;
	for (int i = 0; i <= m; i++) 
		res = min(res, pre[i] + suf[i + 1]);
	if (k == 1)
		res = pre[m];
	cout << res + same_side + m;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 2 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 41 ms 4392 KB Output is correct
13 Correct 115 ms 5876 KB Output is correct
14 Correct 73 ms 4424 KB Output is correct
15 Correct 56 ms 3588 KB Output is correct
16 Correct 55 ms 5236 KB Output is correct
17 Correct 74 ms 5944 KB Output is correct
18 Correct 65 ms 5504 KB Output is correct
19 Correct 79 ms 5948 KB Output is correct
20 Correct 57 ms 5520 KB Output is correct
21 Correct 86 ms 5660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 292 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 3 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 332 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 1 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 43 ms 3640 KB Output is correct
26 Correct 65 ms 3508 KB Output is correct
27 Correct 101 ms 5220 KB Output is correct
28 Correct 99 ms 6008 KB Output is correct
29 Correct 95 ms 5888 KB Output is correct
30 Correct 45 ms 3300 KB Output is correct
31 Correct 48 ms 5292 KB Output is correct
32 Correct 84 ms 5964 KB Output is correct
33 Correct 56 ms 5484 KB Output is correct
34 Correct 79 ms 5964 KB Output is correct
35 Correct 65 ms 5452 KB Output is correct
36 Correct 78 ms 5708 KB Output is correct
37 Correct 40 ms 4408 KB Output is correct