Submission #647430

# Submission time Handle Problem Language Result Execution time Memory
647430 2022-10-02T14:37:53 Z LeonaRaging Feast (NOI19_feast) C++14
4 / 100
487 ms 20280 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;

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

struct Comp {
    bool operator () (int a, int b) const {
        return abs(Rank[a]) < abs(Rank[b]);
    }
};

set<int, Comp> mySet;
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];
            mySet.insert(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;
        mySet.erase(v); ms.erase(v);
        mySet.erase(u);
        Rank[u] += Rank[v];
        mySet.insert(u);
        par[v] = u;
        return 1;
    }
};

void init() {
    while (Rank[*mySet.begin()] < 0) {
        ms.erase(*mySet.begin());
        mySet.erase(*mySet.begin());
    }
    while (Rank[*mySet.rbegin()] < 0) {
        ms.erase(*mySet.rbegin());
        mySet.erase(*mySet.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--;
    a[L - 1] = a[L];
    a[R + 1] = -a[R];
    for (int i = L; i <= R + 1; i++) {
        if (a[i] * a[i - 1] < 0 || (a[i - 1] == 0 && a[i] != 0)) {
            if (cur > 0) {
                cnt++;
            }
            myVec.pb(cur);
            // clog << cur << ' ';
            cur = 0;
        }
        cur += a[i];
    }
    n = myVec.size();
    DisjointSet Dsu(n);
    while (cnt > k) {
        int x = *mySet.begin(), u, v;
        auto lo = ms.lower_bound(x);
        auto hi = ms.upper_bound(x);
        u = (lo == ms.begin() ? x : *(--lo));
        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);
        int suf = Rank[u] > 0;
        cnt -= (pre - suf);
        init();
        // for (auto i : ms) clog << i << ' ';
        // clog << '\n';
        // clog << db(x) << db(u) << db(v) << '\n';
    }
    int res = 0;
    for (auto i : mySet)
        if (Rank[i] > 0) res += Rank[i];
    cout << res;
}
# Verdict Execution time Memory Grader output
1 Correct 105 ms 2856 KB Output is correct
2 Correct 103 ms 2892 KB Output is correct
3 Correct 108 ms 3020 KB Output is correct
4 Correct 108 ms 2892 KB Output is correct
5 Correct 109 ms 2936 KB Output is correct
6 Correct 108 ms 3020 KB Output is correct
7 Correct 103 ms 2852 KB Output is correct
8 Correct 104 ms 2952 KB Output is correct
9 Correct 103 ms 2896 KB Output is correct
10 Correct 106 ms 2888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 60 ms 2944 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 487 ms 20280 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 105 ms 2856 KB Output is correct
2 Correct 103 ms 2892 KB Output is correct
3 Correct 108 ms 3020 KB Output is correct
4 Correct 108 ms 2892 KB Output is correct
5 Correct 109 ms 2936 KB Output is correct
6 Correct 108 ms 3020 KB Output is correct
7 Correct 103 ms 2852 KB Output is correct
8 Correct 104 ms 2952 KB Output is correct
9 Correct 103 ms 2896 KB Output is correct
10 Correct 106 ms 2888 KB Output is correct
11 Incorrect 60 ms 2944 KB Output isn't correct
12 Halted 0 ms 0 KB -