Submission #647438

# Submission time Handle Problem Language Result Execution time Memory
647438 2022-10-02T15:03:48 Z LeonaRaging Feast (NOI19_feast) C++14
4 / 100
1000 ms 20196 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]); 
    }
};

multiset<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;
        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--;
    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;
        mySet.erase(mySet.begin());
        if (ms.find(x) == ms.end()) continue;
        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);
        mySet.insert(u);
        // for (auto i : ms) clog << i << ' '; 
        // clog << '\n';
        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 119 ms 2748 KB Output is correct
2 Correct 106 ms 2800 KB Output is correct
3 Correct 109 ms 2808 KB Output is correct
4 Correct 110 ms 2776 KB Output is correct
5 Correct 118 ms 2864 KB Output is correct
6 Correct 111 ms 2832 KB Output is correct
7 Correct 104 ms 2796 KB Output is correct
8 Correct 106 ms 2852 KB Output is correct
9 Correct 120 ms 2804 KB Output is correct
10 Correct 104 ms 2852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1081 ms 2800 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1085 ms 20196 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1095 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1095 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1095 ms 212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 119 ms 2748 KB Output is correct
2 Correct 106 ms 2800 KB Output is correct
3 Correct 109 ms 2808 KB Output is correct
4 Correct 110 ms 2776 KB Output is correct
5 Correct 118 ms 2864 KB Output is correct
6 Correct 111 ms 2832 KB Output is correct
7 Correct 104 ms 2796 KB Output is correct
8 Correct 106 ms 2852 KB Output is correct
9 Correct 120 ms 2804 KB Output is correct
10 Correct 104 ms 2852 KB Output is correct
11 Execution timed out 1081 ms 2800 KB Time limit exceeded
12 Halted 0 ms 0 KB -