# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
45018 | RezwanArefin01 | Palembang Bridges (APIO15_bridge) | C++17 | 49 ms | 4136 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.
#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
vector<int> pos;
ll sum[100100];
ll get(int l, int r) {
if(l > r) return 0;
ll ret = sum[r];
if(l) ret -= sum[l - 1];
return ret;
}
int main(int argc, char const *argv[]) {
#ifdef LOCAL_TESTING
freopen("in", "r", stdin);
#endif
int n, k; scanf("%d %d", &k, &n);
ll pre = 0;
for(int i = 0; i < n; i++) {
char p, q; int s, t;
scanf(" %c %d %c %d", &p, &s, &q, &t);
if(p == q) pre += abs(s - t);
else {
pos.push_back(s);
pos.push_back(t);
pre++;
}
}
if(!pos.size()) {
printf("%lld\n", pre); exit(0);
}
sort(pos.begin(), pos.end());
n = pos.size();
sum[0] = pos[0];
for(int i = 1; i < n; i++)
sum[i] = sum[i - 1] + pos[i];
if(k == 1) {
ll add = 1e18;
for(int i = 0; i < n; i++) {
ll tot = 1ll * pos[i] * (i + 1) - get(0, i);
tot += get(i + 1, n - 1) - 1ll * pos[i] * (n - i - 1);
add = min(add, tot);
} printf("%lld\n", add + pre);
}
}
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... |