제출 #671563

#제출 시각아이디문제언어결과실행 시간메모리
671563Radin_Zahedi2Palembang Bridges (APIO15_bridge)C++17
22 / 100
58 ms5872 KiB
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;


int n, k;
const int N = 1e5 + 5;
ll arr[N][2];

ll base = 0;

bool cmp(pair<int, int> &a, pair<int, int> &b) {
	return mp(arr[a.fi][a.se], a.se) < mp(arr[b.fi][b.se], b.se);
}

void init() {
}

void input() {
	cin >> k >> n;

	for (int i = 1; i <= n; i++) {
		char a, b;
		cin >> a >> arr[i][0] >> b >> arr[i][1];

		base += abs(arr[i][0] - arr[i][1]);

		if (a == b) {
			i--;
			n--;
		}
		else {
			if (arr[i][0] > arr[i][1]) {
				swap(arr[i][0], arr[i][1]);
			}
			base++;
		}
	}
}

void solve() {
	if (!n) {
		cout << base;
		return;
	}

	pair<int, int> have[2 * n + 1];

	for (int i = 1; i <= n; i++) {
		have[2 * i - 1] = mp(i, 0);
		have[2 * i] = mp(i, 1);
	}

	sort(have + 1, have + 2 * n + 1, cmp);


	ll sum = 0;
	int cnt = -n + 1;
	for (int i = 2; i <= 2 * n; i++) {
		if (have[i].se == 0) {
			sum += arr[have[i].fi][have[i].se];
		}
	}

	ll ans = linf;
	for (int i = 1; i <= 2 * n; i++) {
		int x = arr[have[i].fi][have[i].se];

		ll now = sum + 1ll * cnt * x;
		ans = min(ans, now);

		if (have[i].se == 1) {
			cnt++;
			sum -= arr[have[i].fi][have[i].se];
		}

		if (i < 2 * n && have[i + 1].se == 0) {
			cnt++;
			sum -= arr[have[i + 1].fi][have[i + 1].se];
		}
	}

	cout << base + 2 * ans;
}

void output() {
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int number_of_testcases = 1;
	//cin >> number_of_testcases;
	while (number_of_testcases--) {
		init();

		input();

		solve();

		output();
	}

	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...