답안 #810328

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
810328 2023-08-06T08:29:21 Z vuavisao Feast (NOI19_feast) C++17
100 / 100
70 ms 12152 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);
    }
    if(!List.empty() && List.back() < 0) List.pop_back();
    if(!List.empty() && List.front() < 0) {
        reverse(List.begin(), List.end());
        List.pop_back();
    }
    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
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 2928 KB Output is correct
2 Correct 23 ms 2916 KB Output is correct
3 Correct 23 ms 2968 KB Output is correct
4 Correct 24 ms 3040 KB Output is correct
5 Correct 28 ms 3120 KB Output is correct
6 Correct 25 ms 3164 KB Output is correct
7 Correct 27 ms 3000 KB Output is correct
8 Correct 24 ms 3120 KB Output is correct
9 Correct 23 ms 3088 KB Output is correct
10 Correct 23 ms 3356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 3284 KB Output is correct
2 Correct 16 ms 3064 KB Output is correct
3 Correct 14 ms 2944 KB Output is correct
4 Correct 15 ms 2988 KB Output is correct
5 Correct 23 ms 2872 KB Output is correct
6 Correct 15 ms 2896 KB Output is correct
7 Correct 15 ms 3008 KB Output is correct
8 Correct 23 ms 2928 KB Output is correct
9 Correct 22 ms 2892 KB Output is correct
10 Correct 15 ms 3028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 9412 KB Output is correct
2 Correct 65 ms 9332 KB Output is correct
3 Correct 65 ms 9332 KB Output is correct
4 Correct 64 ms 9308 KB Output is correct
5 Correct 70 ms 9444 KB Output is correct
6 Correct 70 ms 9380 KB Output is correct
7 Correct 64 ms 9416 KB Output is correct
8 Correct 63 ms 9356 KB Output is correct
9 Correct 63 ms 9436 KB Output is correct
10 Correct 68 ms 9456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 324 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 0 ms 212 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 0 ms 332 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 324 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 1 ms 432 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 2928 KB Output is correct
2 Correct 23 ms 2916 KB Output is correct
3 Correct 23 ms 2968 KB Output is correct
4 Correct 24 ms 3040 KB Output is correct
5 Correct 28 ms 3120 KB Output is correct
6 Correct 25 ms 3164 KB Output is correct
7 Correct 27 ms 3000 KB Output is correct
8 Correct 24 ms 3120 KB Output is correct
9 Correct 23 ms 3088 KB Output is correct
10 Correct 23 ms 3356 KB Output is correct
11 Correct 15 ms 3284 KB Output is correct
12 Correct 16 ms 3064 KB Output is correct
13 Correct 14 ms 2944 KB Output is correct
14 Correct 15 ms 2988 KB Output is correct
15 Correct 23 ms 2872 KB Output is correct
16 Correct 15 ms 2896 KB Output is correct
17 Correct 15 ms 3008 KB Output is correct
18 Correct 23 ms 2928 KB Output is correct
19 Correct 22 ms 2892 KB Output is correct
20 Correct 15 ms 3028 KB Output is correct
21 Correct 66 ms 9412 KB Output is correct
22 Correct 65 ms 9332 KB Output is correct
23 Correct 65 ms 9332 KB Output is correct
24 Correct 64 ms 9308 KB Output is correct
25 Correct 70 ms 9444 KB Output is correct
26 Correct 70 ms 9380 KB Output is correct
27 Correct 64 ms 9416 KB Output is correct
28 Correct 63 ms 9356 KB Output is correct
29 Correct 63 ms 9436 KB Output is correct
30 Correct 68 ms 9456 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 328 KB Output is correct
33 Correct 1 ms 328 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 0 ms 212 KB Output is correct
40 Correct 0 ms 340 KB Output is correct
41 Correct 0 ms 340 KB Output is correct
42 Correct 1 ms 332 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 0 ms 340 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 0 ms 324 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 344 KB Output is correct
53 Correct 1 ms 432 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 332 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 1 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 55 ms 12004 KB Output is correct
62 Correct 54 ms 12152 KB Output is correct
63 Correct 62 ms 11868 KB Output is correct
64 Correct 55 ms 12008 KB Output is correct
65 Correct 65 ms 12088 KB Output is correct
66 Correct 60 ms 12008 KB Output is correct
67 Correct 58 ms 11980 KB Output is correct
68 Correct 65 ms 12036 KB Output is correct
69 Correct 63 ms 11996 KB Output is correct
70 Correct 62 ms 11936 KB Output is correct