답안 #959494

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
959494 2024-04-08T10:22:25 Z MinaRagy06 Palembang Bridges (APIO15_bridge) C++17
100 / 100
62 ms 6708 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define sz(x) (int) x.size()

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int k, n;
	cin >> k >> n;
	ll ans = 0;
	vector<array<int, 2>> a;
	for (int i = 0; i < n; i++) {
		char t1, t2;
		int l, r;
		cin >> t1 >> l >> t2 >> r;
		if (t1 == t2) {
			ans += abs(l - r);
			continue;
		}
		if (l > r) swap(l, r);
		a.push_back({l, r});
	}
	n = a.size();
	if (k == 1) {
		vector<ll> v;
		for (int i = 0; i < n; i++) {
			v.push_back(a[i][0]);
			v.push_back(a[i][1]);
		}
		sort(v.begin(), v.end());
		for (int i = 0; i < sz(v); i++) {
			ans += abs(v[i] - v[sz(v) / 2]);
		}
		cout << ans + sz(a) << '\n';
		return 0;
	}
	sort(a.begin(), a.end(), [&] (array<int, 2> x, array<int, 2> y) {return x[0] + x[1] < y[0] + y[1];});
	auto process = [&] (vector<int> &order, ll arr[]) {
		ll sumL = 0, sumR = 0;
		priority_queue<int> pqL;
		priority_queue<int, vector<int>, greater<>> pqR;
		for (auto i : order) {
			pqL.push(a[i][0]);
			sumL += a[i][0];
			pqL.push(a[i][1]);
			sumL += a[i][1];
			while (pqL.size() > pqR.size()) {
				int x = pqL.top();
				pqL.pop();
				sumL -= x;

				pqR.push(x);
				sumR += x;
			}
			while (pqL.top() > pqR.top()) {
				int x = pqL.top(), y = pqR.top();
				pqL.pop();
				sumL -= x;
				pqR.pop();
				sumR -= y;

				pqL.push(y);
				sumL += y;
				pqR.push(x);
				sumR += x;
			}
			assert(pqL.size() == pqR.size());
			arr[i] = sumR - sz(pqR) * pqL.top();
			arr[i] += sz(pqL) * pqL.top() - sumL;
		}
	};
	ll prf[n + 1]{}, suf[n + 1]{};
	vector<int> order(n);
	for (int i = 0; i < n; i++) {
		order[i] = n - 1 - i;
	}
	process(order, suf);
	for (int i = 0; i < n; i++) {
		order[i] = i;
	}
	process(order, prf);
	ll mn = suf[0];
	for (int i = 0; i < n; i++) {
		mn = min(mn, prf[i] + suf[i + 1]);
	}
	ans += mn;
	cout << ans + sz(a) << '\n';
	return 0;
}

# 결과 실행 시간 메모리 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 1 ms 348 KB Output is correct
5 Correct 1 ms 352 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 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 608 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 600 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 436 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 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 21 ms 4188 KB Output is correct
13 Correct 37 ms 5912 KB Output is correct
14 Correct 30 ms 4440 KB Output is correct
15 Correct 20 ms 3292 KB Output is correct
16 Correct 19 ms 5140 KB Output is correct
17 Correct 26 ms 5468 KB Output is correct
18 Correct 26 ms 5068 KB Output is correct
19 Correct 32 ms 5712 KB Output is correct
20 Correct 23 ms 5200 KB Output is correct
21 Correct 29 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 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 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 344 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
12 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 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 1 ms 456 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 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 0 ms 456 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 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 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 496 KB Output is correct
# 결과 실행 시간 메모리 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 392 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 480 KB Output is correct
16 Correct 1 ms 604 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 492 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 26 ms 5068 KB Output is correct
26 Correct 46 ms 5336 KB Output is correct
27 Correct 59 ms 6032 KB Output is correct
28 Correct 62 ms 6608 KB Output is correct
29 Correct 61 ms 6708 KB Output is correct
30 Correct 31 ms 3576 KB Output is correct
31 Correct 31 ms 5756 KB Output is correct
32 Correct 54 ms 6324 KB Output is correct
33 Correct 43 ms 6096 KB Output is correct
34 Correct 60 ms 6256 KB Output is correct
35 Correct 42 ms 5840 KB Output is correct
36 Correct 59 ms 6256 KB Output is correct
37 Correct 19 ms 5076 KB Output is correct