Submission #810326

# Submission time Handle Problem Language Result Execution time Memory
810326 2023-08-06T08:28:36 Z vuavisao Feast (NOI19_feast) C++17
30 / 100
69 ms 9192 KB
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
using namespace std;

const ll N = 3e5 + 10;
const ll INF = 1e18;

struct dsu {
    ll n = 0;
    vector<ll> parent = {};
    
    void resize(ll _n) {
        n = _n; 
        parent.resize(n + 10); for(ll i = 1; i <= n; ++ i) parent[i] = i;
    }
    dsu() {};
    dsu(ll _n) { this -> resize(_n); };

    ll ancestor(ll u) {
        if(parent[u] == u) return u;
        return parent[u] = ancestor(parent[u]);
    }

    bool join(ll u, ll v) {
        u = ancestor(u); v = ancestor(v);
        if(u == v) return false;
        parent[v] = u;
        return true;
    }
};

struct state {
    ll val = 0, idx = 0;
    state() {};
    state(ll _val, ll _idx) : val(_val), idx(_idx) {}; 

    bool operator < (const state& other) const {
        return abs(this -> val) > abs(other.val);
    }
};

ll n, k;
ll a[N];

ll nxt[N];
ll res;

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cin >> n >> k;
    for(ll i = 1; i <= n; ++ i) cin >> a[i];
    vector<ll> List = {};
    for(ll i = 1, j = 1; i <= n; i = j) {
        ll s = 0;
        while(j <= n && a[i] * a[j] >= 0) {
            s += a[j];
            ++ j;
        }
        // cout << s << '\n';
        List.push_back(s);
    }
    n = (ll) List.size();
    ll have_p = 0; 
    priority_queue<state> pq = {};
    for(ll i = 1; i <= n; ++ i) {
        a[i] = List[i - 1];
        nxt[i] = i + 1;
        if(a[i] >= 0) {
            ++ have_p;
        }
        pq.push(state(a[i], i));
    }
    dsu dsu(n);
    while(!pq.empty() && have_p > k) {
        ll val = pq.top().val, idx = pq.top().idx;
        pq.pop();
        if(val != a[idx]) continue;
        if(val >= 0) -- have_p;
        if(idx > 1) {
            ll le = dsu.ancestor(idx - 1); 
            val += a[le];
            if(a[le] >= 0) -- have_p;
            dsu.join(le, idx); 
            a[idx] = - INF; nxt[le] = nxt[idx]; idx = le;
        }
        ll ri = nxt[idx];
        if(ri <= n) {
            val += a[ri];
            if(a[ri] >= 0) -- have_p;
            dsu.join(idx, ri); 
            a[ri] = - INF; nxt[idx] = nxt[ri];
        }
        if(val >= 0) ++ have_p;
        a[idx] = val; 
        if(val < 0 && (idx == 1 || nxt[idx] == n + 1)) continue;
        pq.push(state(val, idx));
    }
    ll res = 0;
    while(!pq.empty()) {
        ll val = pq.top().val, idx = pq.top().idx;
        pq.pop();
        if(val < 0 || val != a[idx]) continue;
        // cout << val << ' ' << idx << '\n';
        res += val;
    }
    cout << res;
    return (0 ^ 0);
}

/// Code by vuavisao
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2620 KB Output is correct
2 Correct 29 ms 2640 KB Output is correct
3 Correct 23 ms 2744 KB Output is correct
4 Correct 23 ms 2848 KB Output is correct
5 Correct 26 ms 2984 KB Output is correct
6 Correct 22 ms 2948 KB Output is correct
7 Correct 22 ms 2976 KB Output is correct
8 Correct 23 ms 2892 KB Output is correct
9 Correct 23 ms 2888 KB Output is correct
10 Correct 23 ms 2972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 3028 KB Output is correct
2 Correct 18 ms 2972 KB Output is correct
3 Correct 15 ms 2708 KB Output is correct
4 Correct 15 ms 2728 KB Output is correct
5 Correct 22 ms 2644 KB Output is correct
6 Correct 18 ms 2640 KB Output is correct
7 Correct 15 ms 2712 KB Output is correct
8 Correct 23 ms 2764 KB Output is correct
9 Correct 22 ms 2608 KB Output is correct
10 Correct 16 ms 2720 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 65 ms 9160 KB Output is correct
2 Correct 69 ms 9120 KB Output is correct
3 Correct 68 ms 9104 KB Output is correct
4 Correct 64 ms 9168 KB Output is correct
5 Correct 64 ms 9160 KB Output is correct
6 Correct 64 ms 9160 KB Output is correct
7 Correct 66 ms 9192 KB Output is correct
8 Correct 63 ms 9080 KB Output is correct
9 Correct 68 ms 9188 KB Output is correct
10 Correct 64 ms 9164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 324 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Incorrect 0 ms 212 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2620 KB Output is correct
2 Correct 29 ms 2640 KB Output is correct
3 Correct 23 ms 2744 KB Output is correct
4 Correct 23 ms 2848 KB Output is correct
5 Correct 26 ms 2984 KB Output is correct
6 Correct 22 ms 2948 KB Output is correct
7 Correct 22 ms 2976 KB Output is correct
8 Correct 23 ms 2892 KB Output is correct
9 Correct 23 ms 2888 KB Output is correct
10 Correct 23 ms 2972 KB Output is correct
11 Correct 15 ms 3028 KB Output is correct
12 Correct 18 ms 2972 KB Output is correct
13 Correct 15 ms 2708 KB Output is correct
14 Correct 15 ms 2728 KB Output is correct
15 Correct 22 ms 2644 KB Output is correct
16 Correct 18 ms 2640 KB Output is correct
17 Correct 15 ms 2712 KB Output is correct
18 Correct 23 ms 2764 KB Output is correct
19 Correct 22 ms 2608 KB Output is correct
20 Correct 16 ms 2720 KB Output is correct
21 Correct 65 ms 9160 KB Output is correct
22 Correct 69 ms 9120 KB Output is correct
23 Correct 68 ms 9104 KB Output is correct
24 Correct 64 ms 9168 KB Output is correct
25 Correct 64 ms 9160 KB Output is correct
26 Correct 64 ms 9160 KB Output is correct
27 Correct 66 ms 9192 KB Output is correct
28 Correct 63 ms 9080 KB Output is correct
29 Correct 68 ms 9188 KB Output is correct
30 Correct 64 ms 9164 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 324 KB Output is correct
34 Correct 0 ms 212 KB Output is correct
35 Incorrect 0 ms 212 KB Output isn't correct
36 Halted 0 ms 0 KB -