답안 #515682

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
515682 2022-01-19T13:06:41 Z keertan Palembang Bridges (APIO15_bridge) C++17
0 / 100
1 ms 332 KB
#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

bool cmp(pair<int, int> a, pair<int, int> b) { return a.first + a.second < b.first + b.second; }

priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int>> rpq;
ll lsum, rsum;

void insert(int x) {
	int median = (lpq.size() ? lpq.top() : 1000000001);
	if (x <= median) {
		lpq.push(x);
		lsum += x;
	} else {
		rpq.push(x);
		rsum += x;
	}

	if (rpq.size() + 1 < lpq.size()) {
		int nxt = lpq.top();
		lpq.pop();
		rpq.push(nxt);
		lsum -= nxt;
		rsum += nxt;
	} else if (lpq.size() < rpq.size()) {
		int nxt = rpq.top();
		rpq.pop();
		lpq.push(nxt);
		rsum -= nxt;
		lsum += nxt;
	}
}

ll pref[100001];

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int k, n;
	ll same_side = 0;
	vector<pair<int, int>> v = {{0, 0}};
	cin >> k >> n;
	FOR(i, 0, n) {
		char a, b;
		int x, y;
		cin >> a >> x >> b >> y;
		if (a == b) same_side += abs(x - y);
		else v.push_back({x, y});
	}
	sort(v.begin(), v.end(), cmp);
	n = v.size() - 1;
	same_side += n;

	lsum = rsum = 0;
	FOR(i, 1, n + 1) {
		insert((v[i].first+v[i].second)/2);
		
		pref[i] = rsum - lsum;
	}

	ll ans = pref[n];

	if (k == 2) {
		while (lpq.size()) lpq.pop();
		while (rpq.size()) rpq.pop();
		lsum = rsum = 0;

		for (int i = n; i; i--) {
		insert((v[i].first+v[i].second)/2);
			ans = min(ans, rsum - lsum + pref[i - 1]);
		}
	}
	cout << same_side + ans;
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Incorrect 1 ms 332 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Incorrect 1 ms 332 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Incorrect 0 ms 328 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Incorrect 0 ms 332 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 0 ms 308 KB Output is correct
3 Incorrect 1 ms 328 KB Output isn't correct
4 Halted 0 ms 0 KB -