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>
#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair
#define int long long
using namespace std;
const int inf = (int)1e15 + 7;
const int N = (int)2e5 + 7;
int k,n;
int x,y;
int suma,sumb;
int pos,pos1;
int ca,cb;
int ans = inf,cur;
char ch,ch1;
vector <int> a,b;
main () {
cin >> k >> n;
for (int i = 1; i <= n; i ++) {
cin >> ch >> x >> ch1 >> y;
if (ch == ch1) {
cur += abs(x - y);
}
else {
if (ch == 'A') a.pb(x),b.pb(y);
else b.pb(x),a.pb(y);
}
}
sort (a.begin(),a.end());
sort (b.begin(),b.end());
if (k == 1) {
for (int i = 0; i < (int)a.size(); i ++)
suma += abs(a[i] - a[0]),sumb += abs(b[i] - a[0]);
for (int j = 0; j < (int)a.size(); j ++) {
int i = a[j];
ans = min(ans,cur + suma + sumb + (int)a.size());
pos = upper_bound(a.begin(),a.end(),a[i]) - a.begin();
suma -= ((int)a.size() - pos);
suma += pos;
pos = upper_bound(b.begin(),b.end(),a[i]) - b.begin();
sumb -= ((int)b.size() - pos);
sumb += pos;
}
suma = sumb = 0;
for (int i = 0; i < (int)a.size(); i ++)
suma += abs(a[i] - b[0]),sumb += abs(b[i] - b[0]);
for (int j = 0; j < (int)b.size(); j ++) {
int i = b[j];
ans = min(ans,cur + suma + sumb + (int)a.size());
pos = upper_bound(a.begin(),a.end(),b[i]) - a.begin();
suma -= ((int)a.size() - pos);
suma += pos;
pos = upper_bound(b.begin(),b.end(),b[i]) - b.begin();
sumb -= ((int)b.size() - pos);
sumb += pos;
}
cout << ans << endl;
}
}
/**
Sample 1
1 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
Sample 2
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
1 3
A 0 B 0
A 1 B 1
A 1000000000 B 1000000000
**/
Compilation message (stderr)
bridge.cpp:26:8: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
# | 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... |