# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
15863 | myungwoo | Palembang Bridges (APIO15_bridge) | C++14 | 2000 ms | 4936 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;
#define MAXN 100005
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
typedef long long lld;
int K, N, M;
struct Z{
int a, b;
} A[MAXN];
lld proc(vector <int> &arr)
{
if (arr.empty()) return 0;
lld ret = 0;
sort(all(arr));
int x = arr[sz(arr)/2];
for (int t: arr) ret += abs(t - x);
return ret;
}
int main()
{
scanf("%d%d", &K, &M);
vector <int> arr;
lld def = 0;
while (M--){
char p, q; int a, b;
scanf(" %c%d %c%d", &p, &a, &q, &b);
if (p == q){
def += abs(a-b);
continue;
}
def++;
if (a > b) swap(a, b);
A[++N] = {a, b};
}
sort(A+1, A+N+1, [](const Z &a, const Z &b){
return a.a+a.b < b.a+b.b;
});
lld ans = 1e18;
for (int i=0;i<=N;i++){
vector <int> arr1, arr2;
for (int j=1;j<=N;j++)
if (j <= i)
arr1.pb(A[j].a), arr1.pb(A[j].b);
else
arr2.pb(A[j].a), arr2.pb(A[j].b);
lld val = def + proc(arr1) + proc(arr2);
ans = min(ans, val);
if (K == 1) break;
}
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... |