Submission #750150

#TimeUsernameProblemLanguageResultExecution timeMemory
750150puppyCake 3 (JOI19_cake3)C++17
24 / 100
4029 ms19980 KiB
#include <iostream>
#include <set>
#include <algorithm>
#include <utility>
using namespace std;
using ll = long long;
const ll inf = 1e18;
pair<ll, int> arr[200005];
int N, M;
int l, r;
set<pair<ll, int>> inc, exc;
ll cur = 0;
void in(int i)
{
    inc.insert(make_pair(arr[i].first, i));
    cur += arr[i].first;
    cur -= (*inc.begin()).first;
    exc.insert(*inc.begin());
    inc.erase(inc.begin());
}
void out(int i)
{
    auto it = inc.find(make_pair(arr[i].first, i));
    if (it != inc.end()) {
        cur -= arr[i].first;
        inc.erase(it);
        cur += (*--exc.end()).first;
        inc.insert(*--exc.end());
        exc.erase(--exc.end());
    }
    else {
        exc.erase(make_pair(arr[i].first, i));
    }
}
void put(int ln, int rn)
{
    while (l-- > ln) in(l);
    while (r++ < rn) in(r);
    while (l < ln) out(l++);
    while (r > rn) out(r--);
}
ll res[200005];
void dnc(int s, int e, int l, int r)
{
    if (s > e) return;
    int m = s + e >> 1;
    int ml = max(m + M - 1, l), mr = r;
    if (ml > mr) {
        dnc(s, m - 1, l, r);
    }
    else {
        int pv = -1;
        for (int i = ml; i <= mr; i++) {
            put(m, i);
            ll tmp = cur - 2 * (arr[i].second - arr[m].second);
            if (res[m] < tmp) {
                res[m] = tmp;
                pv = i;
            }
        }
        dnc(s, m - 1, l, pv);
        dnc(m + 1, e, pv, r);
    }
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cin >> N >> M;
    for (int i = 1; i <= N; i++) {
        cin >> arr[i].first >> arr[i].second;
    }
    fill(res + 1, res + N + 1, -inf);
    sort(arr + 1, arr + N + 1, [&](auto &p1, auto &p2){return p1.second < p2.second;});
    l = 1, r = M;
    for (int i = 1; i <= M; i++) {
        inc.insert(make_pair(arr[i].first, i));
        cur += arr[i].first;
    }
    dnc(1, N-M+1, M, N);
    ll ans = *max_element(res + 1, res + N + 1);
    cout << ans << '\n';
}

Compilation message (stderr)

cake3.cpp: In function 'void dnc(int, int, int, int)':
cake3.cpp:46:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |     int m = s + e >> 1;
      |             ~~^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...