답안 #733636

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
733636 2023-05-01T06:00:14 Z SanguineChameleon Palembang Bridges (APIO15_bridge) C++17
100 / 100
258 ms 14788 KB
#include <bits/stdc++.h>
using namespace std;

void just_do_it();

int main() {
	#ifdef KAMIRULEZ
		freopen("kamirulez.inp", "r", stdin);
		freopen("kamirulez.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	just_do_it();
	return 0;
}

struct median {
	multiset<int> Sl;
	multiset<int> Sr;
	long long left_sum = 0;
	long long right_sum = 0;

	void clear() {
		Sl.clear();
		Sr.clear();
		left_sum = 0;
		right_sum = 0;
	}

	void insert(int x, int y) {
		if (Sr.empty() || x <= *Sr.begin()) {
			Sl.insert(x);
			left_sum += x;
		}
		else {
			Sr.insert(x);
			right_sum += x;
		}
		if (Sr.empty() || y <= *Sr.begin()) {
			Sl.insert(y);
			left_sum += y;
		}
		else {
			Sr.insert(y);
			right_sum += y;
		}
		if (Sl.size() > Sr.size()) {
			left_sum -= *Sl.rbegin();
			right_sum += *Sl.rbegin();
			Sr.insert(*Sl.rbegin());
			Sl.erase(prev(Sl.end()));
		}
		else if (Sl.size() < Sr.size()) {
			right_sum -= *Sr.begin();
			left_sum += *Sr.begin();
			Sl.insert(*Sr.begin());
			Sr.erase(Sr.begin());
		}
	}

	long long get() {
		return right_sum - left_sum;
	};

} med;

void just_do_it() {
	int K, N;
	cin >> K >> N;
	vector<pair<int, pair<int, int>>> A;
	long long add = 0;
	for (int i = 0; i < N; i++) {
		char P, Q;
		int S, T;
		cin >> P >> S >> Q >> T;
		if (P == Q) {
			add += abs(S - T);
		}
		else {
			add++;
			if (K == 1) {
				med.insert(S, T);
			}
			else {
				A.emplace_back(S + T, make_pair(S, T));
			}
		}
	}
	if (K == 1) {
		cout << med.get() + add;
		return;
	}
	if (A.empty()) {
		cout << add;
		return;
	}
	int M = A.size();
	sort(A.begin(), A.end());
	vector<long long> pref(M);
	vector<long long> suf(M);
	for (int i = 0; i < M; i++) {
		int S = A[i].second.first;
		int T = A[i].second.second;
		med.insert(S, T);
		pref[i] = med.get();
	}
	med.clear();
	for (int i = M - 1; i >= 0; i--) {
		int S = A[i].second.first;
		int T = A[i].second.second;
		med.insert(S, T);
		suf[i] = med.get();
	}
	long long res = pref[M - 1];
	for (int i = 0; i < M - 1; i++) {
		res = min(res, pref[i] + suf[i + 1]);
	}
	res += add;
	cout << res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 64 ms 9660 KB Output is correct
13 Correct 133 ms 9672 KB Output is correct
14 Correct 122 ms 8668 KB Output is correct
15 Correct 56 ms 5740 KB Output is correct
16 Correct 72 ms 9584 KB Output is correct
17 Correct 74 ms 9648 KB Output is correct
18 Correct 71 ms 9668 KB Output is correct
19 Correct 112 ms 9688 KB Output is correct
20 Correct 81 ms 9712 KB Output is correct
21 Correct 73 ms 9704 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 316 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 324 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 2 ms 368 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 320 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 320 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 2 ms 212 KB Output is correct
13 Correct 2 ms 340 KB Output is correct
14 Correct 2 ms 340 KB Output is correct
15 Correct 3 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 324 KB Output is correct
18 Correct 2 ms 340 KB Output is correct
19 Correct 2 ms 460 KB Output is correct
20 Correct 2 ms 468 KB Output is correct
21 Correct 2 ms 464 KB Output is correct
22 Correct 2 ms 468 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 2 ms 468 KB Output is correct
25 Correct 153 ms 13204 KB Output is correct
26 Correct 131 ms 13372 KB Output is correct
27 Correct 205 ms 14180 KB Output is correct
28 Correct 229 ms 14788 KB Output is correct
29 Correct 258 ms 14776 KB Output is correct
30 Correct 111 ms 7844 KB Output is correct
31 Correct 149 ms 14112 KB Output is correct
32 Correct 154 ms 14712 KB Output is correct
33 Correct 106 ms 14300 KB Output is correct
34 Correct 131 ms 14716 KB Output is correct
35 Correct 146 ms 14224 KB Output is correct
36 Correct 139 ms 14448 KB Output is correct
37 Correct 132 ms 13236 KB Output is correct