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 ll long long
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int k, n;
cin >> k >> n;
vector<int> a;
vector<pair<ll, ll>> p;
ll res = 0, sum = 0;
for(int i = 0; i < n; i++) {
char c1, c2;
ll a1, a2;
cin >> c1 >> a1 >> c2 >> a2;
if(c1 == c2) {
res += abs(a1-a2);
}
else {
a.push_back(a1);
a.push_back(a2);
p.push_back({a1, a2});
sum += a1 + a2;
}
}
sort(a.begin(), a.end());
if(k == 1) {
int m = a.size()/2;
for(auto i: p) {
res += abs(a[m]-i.first) + abs(a[m]-i.second)+1;
}
}
else {
// dist from middle should be as small as possible
ll mx = sum;
for(int i = 0; i < a.size(); i++) {
ll cur = 0;
int m1 = i/2, m2 = (i+a.size())/2;
//cout << a[m1] << " " << a[m2] << "a\n";
for(auto i: p) {
ll m = (i.first+i.second)/2;
if(abs(a[m1]-m) < abs(a[m2]-m)) {
cur += abs(a[m1]-i.first) + abs(a[m1]-i.second)+1;
}
else {
cur += abs(a[m2]-i.first) + abs(a[m2]-i.second)+1;
}
}
//cout << cur << endl;
mx = min(mx, cur);
}
res += mx;
}
cout << res << "\n";
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:40:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# | 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... |