Submission #647449

# Submission time Handle Problem Language Result Execution time Memory
647449 2022-10-02T15:51:23 Z LeonaRaging Feast (NOI19_feast) C++14
100 / 100
346 ms 19252 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define ll long long
#define pb push_back
#define db(val) "[" #val " = " << (val) << "] "

const ll mod = 1e9 + 7;
const int maxn = 3e5 + 4;
const int INF = 1e9;

using T = pair<int,int>;

int n, k, a[maxn], par[maxn], Rank[maxn];

priority_queue<T, vector<T>, greater<T>> pq;
vector<int> myVec;
set<int> ms;

struct DisjointSet {
    DisjointSet(int n) {
        for (int i = 1; i <= n; i++) {
            par[i] = i, Rank[i] = myVec[i - 1];
            pq.push({abs(Rank[i]), i});
            ms.insert(i);
        }
    }
    int find(int x) {
        if (x != par[x]) par[x] = find(par[x]);
        return par[x];
    }
    bool join(int u, int v) {
        if (u == v) return 0;
        ms.erase(v);
        Rank[u] += Rank[v];
        par[v] = u;
        return 1;
    }
};

void init() {
    while (Rank[*ms.begin()] < 0)
        ms.erase(*ms.begin());  
    while (Rank[*ms.rbegin()] < 0)
        ms.erase(*ms.rbegin());
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0); cout.tie(0);
    //freopen(".INP", "r", stdin);
    //freopen(".OUT", "w", stdout);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int cur = 0, cnt = 0;
    int L = 1, R = n;
    while (a[L] <= 0) L++;
    while (a[R] <= 0) R--;
    int flag = (a[L] > 0 ? 1 : -1);
    for (int i = L; i <= R + 1; i++) {
        if (a[i] * flag < 0 || i == R + 1) {
            if (cur > 0)
                cnt++;
            myVec.pb(cur);
            cur = 0;
            flag = (a[i] > 0 ? 1 : -1);
        }
        cur += a[i];
    }
    n = myVec.size();
    DisjointSet Dsu(n);
    // for (auto i : myVec)
        // clog << i << ' ';
    while (cnt > k) {
        int cur, x; tie(cur, x) = pq.top(); pq.pop();
        if (ms.find(x) == ms.end()) continue;
        auto lo = ms.lower_bound(x);
        auto hi = ms.upper_bound(x);
        int u = (lo == ms.begin() ? x : *(--lo));
        int v = (hi == ms.end() ? x : *hi);
        u = Dsu.find(u); v = Dsu.find(v);
        int pre = (Rank[u] > 0 && u != x) + (Rank[v] > 0 && v != x) + (Rank[x] > 0);
        Dsu.join(x, u);  
        Dsu.join(x, v);
        pq.push({abs(Rank[x]), x});
        int suf = Rank[u] > 0;
        cnt -= (pre - suf);
        init();
        // clog << db(x) << db(u) << db(v) << '\n';
    }
    int res = 0;
    for (auto i : ms)
        if (Rank[i] > 0) res += Rank[i];
    cout << res;
}
# Verdict Execution time Memory Grader output
1 Correct 107 ms 2508 KB Output is correct
2 Correct 108 ms 2536 KB Output is correct
3 Correct 113 ms 2520 KB Output is correct
4 Correct 130 ms 2576 KB Output is correct
5 Correct 106 ms 2500 KB Output is correct
6 Correct 110 ms 2580 KB Output is correct
7 Correct 102 ms 2508 KB Output is correct
8 Correct 116 ms 2536 KB Output is correct
9 Correct 106 ms 2516 KB Output is correct
10 Correct 104 ms 2572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 2560 KB Output is correct
2 Correct 75 ms 2544 KB Output is correct
3 Correct 67 ms 2544 KB Output is correct
4 Correct 58 ms 2508 KB Output is correct
5 Correct 105 ms 2460 KB Output is correct
6 Correct 58 ms 2508 KB Output is correct
7 Correct 54 ms 2616 KB Output is correct
8 Correct 105 ms 2620 KB Output is correct
9 Correct 106 ms 2480 KB Output is correct
10 Correct 74 ms 2580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 315 ms 16036 KB Output is correct
2 Correct 303 ms 19000 KB Output is correct
3 Correct 341 ms 19036 KB Output is correct
4 Correct 312 ms 19000 KB Output is correct
5 Correct 341 ms 19060 KB Output is correct
6 Correct 325 ms 19252 KB Output is correct
7 Correct 327 ms 19152 KB Output is correct
8 Correct 311 ms 19020 KB Output is correct
9 Correct 346 ms 19176 KB Output is correct
10 Correct 327 ms 19120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 404 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 404 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 304 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 404 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 308 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 304 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 2 ms 340 KB Output is correct
22 Correct 2 ms 452 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 468 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 3 ms 340 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 3 ms 340 KB Output is correct
30 Correct 2 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 2508 KB Output is correct
2 Correct 108 ms 2536 KB Output is correct
3 Correct 113 ms 2520 KB Output is correct
4 Correct 130 ms 2576 KB Output is correct
5 Correct 106 ms 2500 KB Output is correct
6 Correct 110 ms 2580 KB Output is correct
7 Correct 102 ms 2508 KB Output is correct
8 Correct 116 ms 2536 KB Output is correct
9 Correct 106 ms 2516 KB Output is correct
10 Correct 104 ms 2572 KB Output is correct
11 Correct 55 ms 2560 KB Output is correct
12 Correct 75 ms 2544 KB Output is correct
13 Correct 67 ms 2544 KB Output is correct
14 Correct 58 ms 2508 KB Output is correct
15 Correct 105 ms 2460 KB Output is correct
16 Correct 58 ms 2508 KB Output is correct
17 Correct 54 ms 2616 KB Output is correct
18 Correct 105 ms 2620 KB Output is correct
19 Correct 106 ms 2480 KB Output is correct
20 Correct 74 ms 2580 KB Output is correct
21 Correct 315 ms 16036 KB Output is correct
22 Correct 303 ms 19000 KB Output is correct
23 Correct 341 ms 19036 KB Output is correct
24 Correct 312 ms 19000 KB Output is correct
25 Correct 341 ms 19060 KB Output is correct
26 Correct 325 ms 19252 KB Output is correct
27 Correct 327 ms 19152 KB Output is correct
28 Correct 311 ms 19020 KB Output is correct
29 Correct 346 ms 19176 KB Output is correct
30 Correct 327 ms 19120 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 404 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 1 ms 212 KB Output is correct
40 Correct 1 ms 308 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 304 KB Output is correct
47 Correct 1 ms 340 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 2 ms 340 KB Output is correct
52 Correct 2 ms 452 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 2 ms 340 KB Output is correct
55 Correct 2 ms 468 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 3 ms 340 KB Output is correct
58 Correct 2 ms 340 KB Output is correct
59 Correct 3 ms 340 KB Output is correct
60 Correct 2 ms 340 KB Output is correct
61 Correct 202 ms 19024 KB Output is correct
62 Correct 170 ms 19124 KB Output is correct
63 Correct 302 ms 18964 KB Output is correct
64 Correct 195 ms 19248 KB Output is correct
65 Correct 236 ms 19152 KB Output is correct
66 Correct 229 ms 19128 KB Output is correct
67 Correct 220 ms 18980 KB Output is correct
68 Correct 332 ms 19072 KB Output is correct
69 Correct 295 ms 18956 KB Output is correct
70 Correct 301 ms 19000 KB Output is correct