# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
763864 | qwerasdfzxcl | Palembang Bridges (APIO15_bridge) | C++17 | 5 ms | 468 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;
typedef long long ll;
ll solve2(vector<pair<int, int>> V){
if (V.empty()) return 0;
ll ret = 4e18;
vector<int> X;
for (auto &[x, y]:V) {X.push_back(x); X.push_back(y);}
sort(X.begin(), X.end());
X.erase(unique(X.begin(), X.end()), X.end());
vector<ll> V2;
for (int i=0;i<(int)X.size();i++){
ll t = 4e18;
for (int j=i+1;j<(int)X.size();j++){
ll val = 0;
for (auto &[x, y]:V){
val += min(abs(x-X[i]) + abs(y-X[i]), abs(x-X[j]) + abs(y-X[j]));
}
ret = min(ret, val);
t = min(t, val);
}
V2.push_back(t);
}
int prv = 0;
for (int i=0;i<(int)V2.size()-1;i++){
int typ;
if (V2[i] > V2[i+1]) typ = 0;
else if (V2[i] == V2[i+1]) typ = 1;
else typ = 2;
assert(prv <= typ);
prv = typ;
}
return ret;
}
int main(){
int k, n;
scanf("%d %d", &k, &n);
ll ans = 0;
vector<pair<int, int>> V;
for (int i=1;i<=n;i++){
char cx, cy;
int px, py;
scanf(" %c %d %c %d", &cx, &px, &cy, &py);
if (cx==cy) {ans += abs(px-py); continue;}
if (cx=='B') swap(px, py);
V.emplace_back(px, py);
ans++;
}
if (k==2) ans += solve2(V);
printf("%lld\n", ans);
}
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... |