Submission #249493

#TimeUsernameProblemLanguageResultExecution timeMemory
249493AMO5Palembang 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 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; bool vis[mxn]; char hs[mxn],of[mxn]; int hos[mxn],off[mxn]; ll solve(int bridge){ ll res = 0ll; for(int i=0; i<n; i++){ if(!vis[i]){ if(off[i]>hos[i]){ if(hos[i]<=bridge&&bridge<=off[i]){ res += off[i]-hos[i]+1; }else if(bridge<hos[i]){ res += hos[i]-bridge+off[i]-bridge+1; }else{ res += bridge-hos[i]+bridge-off[i]+1; } }else{ if(off[i]<=bridge&&bridge<=hos[i]){ res += hos[i]-off[i]+1; }else if(bridge<off[i]){ res += hos[i]-bridge+off[i]-bridge+1; }else{ res += bridge-hos[i]+bridge-off[i]+1; } } } } return res; } 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, mid = 0ll; int cnt = n*2; for(int i=0; i<n; i++){ cin >> hs[i] >> hos[i] >> of[i] >> off[i]; if(hs[i]==of[i]){ ans += ll(abs(hos[i]-off[i])); cnt-=2; vis[i]=1; }else{ mid += ll(hos[i])+ll(off[i]); } } if(k==2)return 0; mid/=cnt; cout << ans+min(solve(mid),solve(mid+1))<< endl; } // 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...