제출 #109960

#제출 시각아이디문제언어결과실행 시간메모리
109960SaboonPalembang Bridges (APIO15_bridge)C++14
22 / 100
57 ms5232 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int maxn = 1e5 + 10;
const ll inf = 1e18;

vector<pair<int, int> > eve;

ll solve(){
	if (eve.empty())
		return 0;
	int n = eve.size();
	vector<ll> a(2 * n + 1);
	for (int i = 0; i < n; i++){
		a[2 * i + 0 + 1] = eve[i].first;
		a[2 * i + 1 + 1] = eve[i].second;
	}
	sort(a.begin() + 1, a.end());
	n = 2 * n;
	for (int i = 1; i <= n; i++)
		a[i] += a[i - 1];
	ll answer = inf;
	for (int i = 1; i <= n; i++){
		ll T = a[i] - a[i - 1];
		answer = min(answer, T * (i - (n - i)) - a[i] + (a[n] - a[i])); 
	}
	return answer + eve.size();
}

int main(){
	ios_base::sync_with_stdio(false);
	int k, n;
	cin >> k >> n;
	ll answer = 0;
	for (int i = 0; i < n; i++){
		int x, y;
		char a, b;
		cin >> a >> x >> b >> y;
		if (a == b){
			answer += abs(x - y);
			continue;
		}
		if (a == 'B')
			swap(x, y);
		eve.push_back({x, y});
	}
	if (k == 1){
		cout << answer + solve() << '\n';
		return 0;
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...