# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1037806 | KasymK | Palembang Bridges (APIO15_bridge) | C++17 | 2074 ms | 9988 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 ff first
#define ss second
#define all(v) v.begin(), v.end()
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define wr puts("----------------")
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
const int MOD = 1e9+7;
const int INF = 1e9;
const int N = 1e5+5;
int s[N], t[N];
char p[N], q[N];
int main(){
int k, n;
scanf("%d%d", &k, &n);
if(k == 1){
ll sm = 0, sum = 0;
int cnt = 0;
multiset<int> A;
for(int i = 1; i <= n; ++i){
char p, q;
int s, t;
scanf(" %c %d %c %d", &p, &s, &q, &t);
if (p == q)
sm += abs(s-t);
else{
cnt++;
A.insert(s);
A.insert(t);
sum -= (s+t);
}
}
ll answer = cnt+sm;
while(cnt--)
sum += (*--A.end()*2), A.erase(--A.end());
answer += sum;
printf("%lld\n", answer);
return 0;
}
// k=2
for(int i = 1; i <= n; ++i){
char a, b;
int x, y;
scanf(" %c %d %c %d", &a, &x, &b, &y);
p[i] = a, s[i] = x, q[i] = b, t[i] = y;
}
ll answer = 1e18;
for(int x = 1; x <= n; ++x)
for(int y = 1; y <= n; ++y){
if(s[x] == t[y])
continue; // two bridge cannot be in same place
ll sm = 0;
int br = s[x], br_ = t[y];
for(int i = 1; i <= n; ++i){
if(p[i] == q[i]){
sm += abs(s[i]-t[i]);
continue;
}
sm++;
ll A = abs(br-s[i])+abs(br-t[i]);
ll B = abs(br_-s[i])+abs(br_-t[i]);
sm += min(A, B);
}
umin(answer, sm);
sm = 0;
if(s[y] == t[x])
continue;
br = s[y], br_ = t[x];
for(int i = 1; i <= n; ++i){
if(p[i] == q[i]){
sm += abs(s[i]-t[i]);
continue;
}
sm++;
ll A = abs(br-s[i])+abs(br-t[i]);
ll B = abs(br_-s[i])+abs(br_-t[i]);
sm += min(A, B);
}
umin(answer, sm);
}
printf("%lld\n", answer);
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... |