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;
typedef long long ll;
constexpr int INF = 1e9;
struct DS{
priority_queue<pair<int, int>> L;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> R;
int mid, cnt1, cnt0;
ll sum1, sum0;
void init(){
while(!L.empty()) L.pop();
while(!R.empty()) R.pop();
mid = cnt1 = cnt0 = 0;
sum1 = sum0 = 0;
}
void insert(int x, int typ){
if (x <= mid){
L.emplace(x, typ);
if (typ==1) sum1 += x, cnt1++;
}
else{
R.emplace(x, typ);
if (typ==0) sum0 += x, cnt0++;
}
}
ll calc(){
while(L.size() > R.size()){
if (L.top().second==0) sum0 += L.top().first, cnt0++;
else sum1 -= L.top().first, cnt1--;
R.push(L.top()); L.pop();
}
while(L.size() < R.size()){
if (R.top().second==0) sum0 -= R.top().first, cnt0--;
else sum1 += R.top().first, cnt1++;
L.push(R.top()); R.pop();
}
mid = L.top().first;
return (ll)mid * (cnt1 - cnt0) - sum1 + sum0;
}
}D;
ll solve1(vector<pair<int, int>> V){
return 0;
}
vector<ll> calc(vector<pair<int, int>> V){
vector<int> X;
for (auto &[x, y]:V){
X.push_back(x); X.push_back(y);
if (x > y) swap(x, y);
}
sort(V.begin(), V.end(), [](const pair<int, int> &x, const pair<int, int> &y){return x.first + x.second < y.first + y.second;});
vector<ll> ret = {0};
D.init();
for (int i=0;i<(int)V.size();i++){
D.insert(V[i].first, 0);
D.insert(V[i].second, 1);
ret.push_back(D.calc());
}
return ret;
}
ll solve2(vector<pair<int, int>> V){
ll ret = 4e18, lenS = 0;
for (auto &[x, y]:V) lenS += abs(x-y);
auto L = calc(V);
for (auto &[x, y]:V) x = INF-x, y = INF-y;
auto R = calc(V);
reverse(R.begin(), R.end());
for (int i=0;i<(int)L.size();i++){
ret = min(ret, L[i] + R[i]);
}
return ret * 2 + lenS;
}
int main(){
int k, n;
scanf("%d %d", &k, &n);
ll ans = 0;
vector<pair<int, int>> V;
for (int i=1;i<=n;i++){
char cx, cy;
int px, py;
scanf(" %c %d %c %d", &cx, &px, &cy, &py);
if (cx==cy) {ans += abs(px-py); continue;}
if (cx=='B') swap(px, py);
V.emplace_back(px, py);
ans++;
}
if (k==2) ans += solve2(V);
else ans += solve1(V);
printf("%lld\n", ans);
}
Compilation message (stderr)
bridge.cpp: In function 'int main()':
bridge.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d %d", &k, &n);
| ~~~~~^~~~~~~~~~~~~~~~~
bridge.cpp:104:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | scanf(" %c %d %c %d", &cx, &px, &cy, &py);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |