# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
362678 | hoaphat1 | Cake 3 (JOI19_cake3) | C++17 | 1499 ms | 38500 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;
int main() {
#define qwer "test"
if (fopen(qwer".inp","r")) freopen(qwer".inp","r",stdin),freopen(qwer".out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++) cin >> a[i].second >> a[i].first;
sort(a.begin(), a.end());
/*
k1 k2 ... km -> v - 2 * (c km - c k1)
i >= i-1
*/
long long ans = -1e18;
long long now = 0;
int L = 0, R = -1;
vector<int> kt(n, 0);
priority_queue<pair<int, int>, vector<pair<int,int>> , greater<pair<int, int>>> pq;
priority_queue<pair<int, int>> smaller;
int sz = 0;
auto add = [&](int id) {
int v = a[id].second;
pq.emplace(v, id);
now += v;
kt[id] = 1;
++sz;
while (sz > m - 2) {
auto x = pq.top();
pq.pop();
if (kt[x.second] != 1) continue;
kt[x.second] = 2;
now -= x.first;
smaller.push(x);
--sz;
}
};
auto del = [&](int id) {
int v = a[id].second;
if (kt[id] == 2) {
kt[id] = 0;
return ;
}
kt[id] = 0;
now -= v;
sz--;
while (sz < m - 2 && !smaller.empty()) {
auto x = smaller.top(); smaller.pop();
if (kt[x.second] != 2) continue;
kt[x.second] = 1;
now += x.first;
pq.push(x);
++sz;
}
};
auto moveL = [&](int l) {
while (L < l) del(L++);
while (L > l) add(--L);
};
auto moveR = [&](int r) {
while (R < r) add(++R);
while (R > r) del(R--);
};
function<void(int, int, int, int)> solve = [&](int l, int r, int L, int R) {
if (l > r) return ;
int mid = l + r >> 1;
int pos = -1;
moveR(mid - 1);
long long val = 0;
for (int i = min(mid-1, R); i >= L; i--) {
moveL(i + 1);
/// cout << i + 1 <<" " << mid - 1 <<" " << now << endl;
if (sz == m - 2) {
if (val < now + a[i].second + 2 * a[i].first) {
val = now + a[i].second + 2 * a[i].first;
pos = i;
}
}
}
ans = max(ans, val + a[mid].second - 2 * a[mid].first);
solve(mid + 1, r, pos, R);
solve(l, mid - 1, L, pos);
};
solve(m-1, n-1, 0, n-1);
cout << ans;
}
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... |