Submission #971646

#TimeUsernameProblemLanguageResultExecution timeMemory
971646Halym2007Palembang Bridges (APIO15_bridge)C++17
22 / 100
35 ms9688 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz size()
const int N = 1e5 + 5;
ll jogap, a[N], b[N], p[2 * N], q[2 * N];
int n, k;
char oy[N], of[N];
vector <ll> v;
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];
	}
	ll goshmaly = 0;
	jogap = 2e18;
	for (int i = 1; i <= n; ++i) {
		if (oy[i] == of[i]) {
			goshmaly += abs (a[i] - b[i]);
			
		}
		else {
			v.pb (a[i]);
			v.pb (b[i]);
		} 	
	}
	goshmaly += (ll)v.sz / 2;
//	cout << goshmaly;
//	return 0;
	sort (v.begin(), v.end());
	ll git = (ll)v.sz;
	
	for (ll i = 1; i < git; ++i) {
		p[i] = p[i - 1] + (v[i] - v[i - 1]) * i; 
	}
//	for (int i = 0; i < git; ++i) {
//		cout << "bashdaky -> " << i << " " << v[i] << " " << p[i] << "\n";
//	}
//	return 0;
//	cout << "\n\n";
	for (int i = git - 2; i >= 0; i--) {
		q[i] = q[i + 1] + (v[i + 1] - v[i]) * (git - i - 1);
	}
//	for (int i = 0; i < git; ++i) {
//		cout << "sondaky -> " << i << " " << v[i] << " " << q[i] << "\n";
//	}
	for (int i = 0; i < git; ++i) {
		jogap = min (jogap, p[i] + q[i]);
	}
	if (jogap == 2e18) jogap = 0;
//	cout << jogap << " " << goshmaly << " ";
	cout << jogap + goshmaly;
}
#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...