Submission #60586

#TimeUsernameProblemLanguageResultExecution timeMemory
60586istleminPalembang Bridges (APIO15_bridge)C++14
0 / 100
4 ms800 KiB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(ll i = a; i < ll(b); ++i)
#define trav(a, v) for(auto& a : v)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define D(x) cerr << #x << " = " << x << endl

typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;


int main() {
    cin.sync_with_stdio(false);
	ll k,n;
	ll totDist = 0;

	vector<ll> points;
	cin>>k>>n;
	rep(i,0,n){
		char a,b;
		ll c,d;
		cin>>a>>c>>b>>d;
		if(a!=b){
			points.push_back(c);
			points.push_back(d);
			totDist++;
		}else{
			totDist += abs(c-d);
		}
	}
	ll bestDist = 1e18;
	
	rep(i,0,points.size()){
		ll dist = 0;
		rep(j,0,points.size()) dist += abs(points[j]-points[i]);
		bestDist = min(dist,bestDist);
	}

	sort(all(points));
	ll ahead = points.size()-1;
	ll behind = 1;
	ll dist = 0;
	rep(i,1,points.size()) dist += points[i]-points[0];
	ll bestDist2 = dist;
	rep(i,1,points.size()) {
		ll jump = points[i]-points[i-1];
		dist += jump*behind;
		dist -= jump*ahead;
		bestDist2 = min(dist,bestDist2);
		++behind;
		--ahead;
	}

	assert(bestDist2==bestDist);

	totDist += bestDist;
	cout<<totDist<<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...