Submission #647451

# Submission time Handle Problem Language Result Execution time Memory
647451 2022-10-02T15:52:14 Z LeonaRaging Feast (NOI19_feast) C++14
100 / 100
311 ms 16152 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);
    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();
    }
    int res = 0;
    for (auto i : ms)
        if (Rank[i] > 0) res += Rank[i];
    cout << res;
}
# Verdict Execution time Memory Grader output
1 Correct 41 ms 2540 KB Output is correct
2 Correct 31 ms 2624 KB Output is correct
3 Correct 29 ms 2596 KB Output is correct
4 Correct 29 ms 2576 KB Output is correct
5 Correct 28 ms 2540 KB Output is correct
6 Correct 27 ms 2516 KB Output is correct
7 Correct 30 ms 2508 KB Output is correct
8 Correct 35 ms 2588 KB Output is correct
9 Correct 35 ms 2564 KB Output is correct
10 Correct 28 ms 2548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2516 KB Output is correct
2 Correct 19 ms 2600 KB Output is correct
3 Correct 18 ms 2516 KB Output is correct
4 Correct 22 ms 2548 KB Output is correct
5 Correct 33 ms 2480 KB Output is correct
6 Correct 29 ms 2576 KB Output is correct
7 Correct 37 ms 2664 KB Output is correct
8 Correct 33 ms 2596 KB Output is correct
9 Correct 30 ms 2516 KB Output is correct
10 Correct 20 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 293 ms 16060 KB Output is correct
2 Correct 250 ms 16008 KB Output is correct
3 Correct 311 ms 16016 KB Output is correct
4 Correct 249 ms 16092 KB Output is correct
5 Correct 228 ms 16104 KB Output is correct
6 Correct 280 ms 16104 KB Output is correct
7 Correct 217 ms 16072 KB Output is correct
8 Correct 241 ms 16116 KB Output is correct
9 Correct 250 ms 16152 KB Output is correct
10 Correct 295 ms 16144 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 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 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 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 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 340 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 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 0 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 340 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 1 ms 340 KB Output is correct
22 Correct 1 ms 456 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 2 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 2 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 2540 KB Output is correct
2 Correct 31 ms 2624 KB Output is correct
3 Correct 29 ms 2596 KB Output is correct
4 Correct 29 ms 2576 KB Output is correct
5 Correct 28 ms 2540 KB Output is correct
6 Correct 27 ms 2516 KB Output is correct
7 Correct 30 ms 2508 KB Output is correct
8 Correct 35 ms 2588 KB Output is correct
9 Correct 35 ms 2564 KB Output is correct
10 Correct 28 ms 2548 KB Output is correct
11 Correct 19 ms 2516 KB Output is correct
12 Correct 19 ms 2600 KB Output is correct
13 Correct 18 ms 2516 KB Output is correct
14 Correct 22 ms 2548 KB Output is correct
15 Correct 33 ms 2480 KB Output is correct
16 Correct 29 ms 2576 KB Output is correct
17 Correct 37 ms 2664 KB Output is correct
18 Correct 33 ms 2596 KB Output is correct
19 Correct 30 ms 2516 KB Output is correct
20 Correct 20 ms 2644 KB Output is correct
21 Correct 293 ms 16060 KB Output is correct
22 Correct 250 ms 16008 KB Output is correct
23 Correct 311 ms 16016 KB Output is correct
24 Correct 249 ms 16092 KB Output is correct
25 Correct 228 ms 16104 KB Output is correct
26 Correct 280 ms 16104 KB Output is correct
27 Correct 217 ms 16072 KB Output is correct
28 Correct 241 ms 16116 KB Output is correct
29 Correct 250 ms 16152 KB Output is correct
30 Correct 295 ms 16144 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 1 ms 340 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 0 ms 340 KB Output is correct
40 Correct 1 ms 340 KB Output is correct
41 Correct 1 ms 340 KB Output is correct
42 Correct 0 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 340 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 1 ms 340 KB Output is correct
52 Correct 1 ms 456 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 468 KB Output is correct
56 Correct 2 ms 340 KB Output is correct
57 Correct 1 ms 340 KB Output is correct
58 Correct 1 ms 340 KB Output is correct
59 Correct 2 ms 340 KB Output is correct
60 Correct 1 ms 340 KB Output is correct
61 Correct 107 ms 16080 KB Output is correct
62 Correct 75 ms 16080 KB Output is correct
63 Correct 210 ms 16000 KB Output is correct
64 Correct 133 ms 16124 KB Output is correct
65 Correct 142 ms 16140 KB Output is correct
66 Correct 150 ms 16092 KB Output is correct
67 Correct 163 ms 16088 KB Output is correct
68 Correct 229 ms 16044 KB Output is correct
69 Correct 211 ms 15980 KB Output is correct
70 Correct 211 ms 16004 KB Output is correct