제출 #882423

#제출 시각아이디문제언어결과실행 시간메모리
882423vjudge1Palembang Bridges (APIO15_bridge)C++11
0 / 100
0 ms348 KiB
#include<bits/stdc++.h>
#define all(aa) aa.begin(), aa.end()
#define endl ("\n")
#define pb push_back
typedef long long ll;
const int maxn = 1e5 + 5;
using namespace std;

int n, k;
vector<pair<ll, ll>> v;
vector<ll> u;
multiset<ll> vis;
multiset<pair<ll, ll>> come; // vis r'ye gore artan come l'ye gore artan
ll lft, rgt, lst;
ll ans = LONG_LONG_MAX, cur;

int main(){
	cin >> k >> n;
	for(int i = 0; i < n; i++){
		char c1, c2;
		int a, b;
		cin >> c1 >> a >> c2 >> b;
		if(b > a) swap(a, b);
		cur+=a-b;
		if(c1!=c2){
			++cur;
			v.pb({b, a});
			u.pb(a), u.pb(b);
			come.insert({b, a});
			cur+=2*b;
		}
	}
	rgt = n = v.size();
	sort(all(v));
	sort(all(u));
	for(int i = 0; i < 2*n; i++){
		cur += lft*2*(u[i] - lst) - rgt*2*(u[i] - lst);
		if((*come.begin()).first == u[i]){
			pair<ll, ll> p = *come.begin();
			vis.insert({p.second});
			come.erase(come.find(p));
			rgt--;
		} 
		if(*vis.begin() == u[i]){
			lft++;
			vis.erase(vis.find(u[i]));
		}
		ans = min(ans, cur);
		lst = u[i];
	}

	cout << ans << endl;

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