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 sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
priority_queue<int> L;
priority_queue<int, vector<int>, greater<int> > R;
ll sumL = 0, sumR = 0;
void insert(int x) {
int med = (L.empty() ? 1e9 + 1 : L.top());
if(x <= med) L.push(x), sumL += x;
else R.push(x), sumR += x;
if(L.size() > R.size() + 1) {
R.push(L.top());
sumL -= L.top();
sumR += L.top();
L.pop();
} else if(R.size() > L.size()) {
L.push(R.top());
sumL += R.top();
sumR -= R.top();
R.pop();
}
}
int32_t main() {
int k, n;
cin >> k >> n;
ll ans = 0, ans2 = 0;
vector<pii> v;
v.push_back({ 0, 0 });
for(int i=0; i<n; i++) {
char a, b;
int c, d;
cin >> a >> c >> b >> d;
if(a == b) {
ans2 += abs(c - d);
} else {
v.push_back({ c, d });
ans2++;
}
}
if(v.size() == 0) {
cout << ans2 << '\n';
return 0;
}
if(k == 1) {
for(int i=1; i<v.size(); i++) {
insert(v[i].first);
insert(v[i].second);
}
ans = sumR - sumL;
} else {
ans = 1e18;
sort(1+all(v), [&](pii &a, pii &b) { return (a.first + a.second) < (b.first + b.second); });
for(int i=1; i<v.size(); i++) {
ll res = 0;
sumL = sumR = 0;
while(!L.empty()) L.pop();
while(!R.empty()) R.pop();
for(int j=1; j<=i; j++) {
insert(v[j].first);
insert(v[j].second);
}
res += sumR - sumL;
sumL = sumR = 0;
while(!L.empty()) L.pop();
while(!R.empty()) R.pop();
for(int j=i+1; j<v.size(); j++) {
insert(v[j].first);
insert(v[j].second);
}
res += sumR - sumL;
ans = min(ans, res);
}
}
cout << ans + ans2 << '\n';
return 0;
}
Compilation message (stderr)
bridge.cpp: In function 'int32_t main()':
bridge.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i=1; i<v.size(); i++) {
| ~^~~~~~~~~
bridge.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int i=1; i<v.size(); i++) {
| ~^~~~~~~~~
bridge.cpp:99:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for(int j=i+1; j<v.size(); j++) {
| ~^~~~~~~~~
# | 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... |