Submission #249557

#TimeUsernameProblemLanguageResultExecution timeMemory
249557AMO5Palembang Bridges (APIO15_bridge)C++17
0 / 100
1 ms512 KiB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define eb emplace_back
#define mt make_tuple
#define all(x) (x).begin(), (x).end()  
#define sz(x) int(x.size()) 
#define MOD 1000000007

typedef long long ll;
typedef pair <int, int> ii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;

const ll INF=LLONG_MAX;
const int mxn=1e5+5;
bool DEBUG=0;
int n,k;
vll coor;
vector<pll>str;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    //freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
	cin >> k >> n;
	ll ans = 0ll;
	for(int i=0; i<n; i++){
		char hs,of; ll hos,off;
		cin >> hs >> hos >> of >> off;
		if(hs==of){
			ans += (abs(hos-off));
		}else{
			if(hos>off)swap(hos,off);
			ans+=off-hos+1ll;
			str.eb(hos,off);
			coor.eb(hos);
			coor.eb(off);
		}
	}
	sort(all(coor));
	//k=1
	int bridge1=coor[sz(str)-1],bridge2=coor[sz(str)];
	ll res1=0ll,res2=0ll;
	for(int i=0; i<sz(str); i++){
		if(bridge1<str[i].fi){
			res1 += 2ll*(str[i].fi-bridge1);
		}else if(bridge1>str[i].se){
			res1 += 2ll*(bridge1-str[i].se);
		}
		if(bridge2<str[i].fi){
			res2 += 2ll*(str[i].fi-bridge2);
		}else if(bridge2>str[i].se){
			res2 += 2ll*(bridge2-str[i].se);
		}
	}
	ans += min(res1,res2);
	cout << ans << '\n';
}
	
// READ & UNDERSTAND
// ll, int overflow, array bounds, memset(0)
// special cases (n=1?), n+1 (1-index)
// do smth instead of nothing & stay organized
// WRITE STUFF DOWN
#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...