# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
838961 | drkarlicio2107 | Palembang Bridges (APIO15_bridge) | C++14 | 1 ms | 336 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
vector < pair <long long int, int> > v;
long long int pos [20010][2];
int main (){
int n, k; cin >> k >> n; long long int sol=0; int ind=0;
for (int i=0; i<n; i++){
long long int a, b; char x, y; cin >> x >> a >> y >> b;
if (x==y){
sol+=abs (a-b); continue;
}
v.push_back ({a, 0});
v.push_back ({b, 1}); pos [ind][0]=a; pos [ind][1]=b; ind++;
}
long long int ans=1e18;
for (int i=0; i<v.size(); i++){
int po=v [i].first; long long int curr=0;
for (int j=0; j<ind; j++){
if (!(po<=pos [j][1] && po>=pos [j][0])) curr+=(abs (pos [j][0]-po)+abs (pos [j][1]-po)+1);
else curr+=(abs (pos [j][0]-pos [j][1])+1);
}
ans=min (ans, curr);
po=v [i].first-1; curr=0;
for (int j=0; j<ind; j++){
if (!(po<=pos [j][1] && po>=pos [j][0])) curr+=(abs (pos [j][0]-po)+abs (pos [j][1]-po)+1);
else curr+=(abs (pos [j][0]-pos [j][1])+1);
}
ans=min (ans, curr);
po=v [i].first+1; curr=0;
for (int j=0; j<ind; j++){
if (!(po<=pos [j][1] && po>=pos [j][0])) curr+=(abs (pos [j][0]-po)+abs (pos [j][1]-po)+1);
else curr+=(abs (pos [j][0]-pos [j][1])+1);
}
ans=min (ans, curr);
//cout << ans << endl;
}
cout << sol+ans << endl;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |