Submission #971621

#TimeUsernameProblemLanguageResultExecution timeMemory
971621Halym2007Palembang Bridges (APIO15_bridge)C++17
8 / 100
2058 ms2908 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
ll jogap, a[N], b[N];
int n, k;
char oy[N], of[N];
ll hasapla (ll x, ll y) {
	ll ret = 0;
	for (int i = 1; i <= n; ++i) {
		if (oy[i] == of[i]) {
			ret += abs (a[i] - b[i]);
		}
		else {
			ll gosh = abs (a[i] - x) + abs (x - b[i]) + 1;
			ll gosh1 = 2e18;
			if (y != -1) {
				gosh1 = abs (a[i] - y) + abs (y - b[i]) + 1;
			}
			ret += min (gosh, gosh1);
			
		}
	}
	return ret;
}

int main () {
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//	freopen ("input.txt", "r", stdin);
	cin >> k >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> oy[i] >> a[i] >> of[i] >> b[i];
	}
//	return cout << n << " " << k, 0;
//	for (int i = 1; i <= n; ++i) {
//		cout << i << " --> " << oy[i] << " " << a[i] << " " << of[i] << " " << b[i] << "\n";
//	}
//	return 0;
	jogap = 2e18;
	for (int i = 1; i <= n; ++i) {
		if (k == 1) jogap = min ({jogap, hasapla(a[i], -1), hasapla (b[i], -1)});
		else {
			for (int j = i + 1; j <= n; ++j) {
				jogap = min ({jogap, hasapla(a[i], a[j]), hasapla (a[i], b[j]), hasapla (a[i], b[i]), hasapla (b[i], a[j]), hasapla (b[i], b[j]), hasapla (a[j], b[j])});
			}
		}
	}
	cout << jogap;
}
#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...