# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
951084 | Amaarsaa | Palembang Bridges (APIO15_bridge) | C++14 | 37 ms | 5796 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;
using ll = long long ;
ll Pre[100005], Suf[100005];
void DBack(multiset<ll>& A) {
auto P = A.end();
P --;
A.erase(P);
return ;
}
ll Ed(multiset<ll>&A) {
auto P = A.end();
P --;
return *P;
}
bool cmp(auto& A, auto& B) {
return A.first + A.second < B.first + B.second;
}
multiset < ll > deer, door;
ll deer_sum =0, door_sum =0;
void Insert(ll x) {
if ( door.size() == deer.size()) {
if (deer.empty() || *deer.begin() >= x) door.insert(x), door_sum += x;
else {
door_sum += *deer.begin();
deer_sum -= *deer.begin();
deer_sum += x;
door.insert(*deer.begin());
deer.erase(deer.begin());
deer.insert(x);
}
}
else {
if ( Ed(door) <= x) {
deer.insert(x);
deer_sum += x;
}
else {
deer_sum += Ed(door);
door_sum -= Ed(door);
door_sum += x;
deer.insert(Ed(door));
DBack(door);
door.insert(x);
}
}
}
int main() {
// freopen("hayfeast.in", "r", stdin);
// freopen("hayfeast.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
ll t, n, m, ans, s,sum, x, y, r, p, i, j, cnt = 0;
cin >> n >> m;
ans = 0;
sum = 0;
char ch1, ch2;
vector < ll > v;
vector < pair < ll, ll > > q;
for (i = 1; i <= m; i ++) {
cin >> ch1 >> x >> ch2 >> y;
if ( ch1 == ch2) {
ans += abs(x - y);
sum += abs(x - y);
}
else {
v.push_back(x);
v.push_back(y);
q.push_back({x, y});
}
}
sort(v.begin(), v.end());
s = v.size();
s = (s + 1)/2 - 1;
if ( n == 2) {
sort(q.begin(), q.end());
for (i = 0; i < q.size(); i ++) {
Insert(q[i].first);
Insert(q[i].second);
if ( i % 2 == 0) s = Ed(door) + (deer_sum - door_sum);
else s = deer_sum - door_sum;
Pre[i] = s;
}
door.clear();
deer.clear();
deer_sum = door_sum =0;
ans = Pre[q.size() - 1];
for (i = q.size() - 1; i >= 0; i --) {
Insert(q[i].first);
Insert(q[i].second);
if ( i % 2 == 0) s = Ed(door) + (deer_sum - door_sum);
else s = deer_sum - door_sum;
Suf[i] = s;
if (i == 0) ans = min(ans, Suf[i]);
else ans =min(ans, Suf[i] + Pre[i - 1]);
}
cout << ans + q.size() + sum<< endl;
return 0;
}
for (i = 0; i < v.size(); i ++) {
ans = ans + abs(v[s] - v[i]);
}
cout << ans + v.size()/2<< endl;
}
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... |