답안 #693150

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
693150 2023-02-02T12:23:00 Z nhphucqt Feast (NOI19_feast) C++17
100 / 100
261 ms 18520 KB
#include <bits/stdc++.h>

using namespace std;

template<typename T>
inline void maximize(T&x, T y) {
    if (x < y) x = y;
}

const int N = 3e5+7;
int n, k, a[N];
vector<long long> v;
int fa[N];
long long sum[N];
pair<int,int> range[N];
int cnt, lef, rig;
set<pair<long long,int>> comp;
long long res;

int root(int x) {
    return fa[x] < 0 ? x : fa[x] = root(fa[x]);
}

void unite(int u, int v) {
    if (fa[u] > fa[v]) swap(u,v);
    comp.erase(make_pair(abs(sum[u]), u));
    comp.erase(make_pair(abs(sum[v]), v));
    fa[u] += fa[v];
    fa[v] = u;
    sum[u] += sum[v];
    range[u].first = min(range[u].first, range[v].first);
    range[u].second = max(range[u].second, range[v].second);
}

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    //freopen("PHATQUA.inp", "r", stdin);
    //freopen("PHATQUA.out", "w", stdout);

    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    long long s = 0;
    for (int i = 1; i <= n; ++i) {
        s += a[i];
        if (i==n || ((a[i]>0)^(a[i+1]>0))) {
            v.push_back(s);
            s = 0;
        }
    }
    memset(fa, -1, sizeof fa);
    for (int i = 0; i < (int)v.size(); ++i) {
        sum[i] = v[i];
        range[i] = make_pair(i,i);
    }
    for (int i = 0; i < (int)v.size(); ++i) {
        if (v[i] > 0 || (i>0&&i+1<(int)v.size())) {
            comp.emplace(abs(v[i]), i);
        }
        cnt += v[i] > 0;
    }
    lef = v[0] <= 0 ? 1 : 0;
    rig = v.back() <= 0 ? (int)v.size()-2 : (int)v.size()-1;
    while (cnt > k) {
        int u = comp.begin()->second;
        assert(u == root(u));
        cnt -= sum[u] > 0;
        if (range[u].first > lef) {
            int v = root(range[u].first-1);
            cnt -= sum[v] > 0;
            unite(u, v);
        }
        u = root(u);
        if (range[u].second < rig) {
            int v = root(range[u].second+1);
            cnt -= sum[v] > 0;
            unite(u, v);
        }
        u = root(u);
        if (sum[u] < 0 && (range[u].first == lef || range[u].second == rig)) {
            if (range[u].first == lef) {
                lef = range[u].second+1;
            }
            else {
                rig = range[u].first-1;
            }
        }
        else {
            comp.emplace(abs(sum[u]), u);
            cnt += sum[u] > 0;
        }
    }
    for (auto p : comp) {
        if (sum[p.second] > 0) {
            res += sum[p.second];
        }
    }
    cout << res;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5376 KB Output is correct
2 Correct 37 ms 5392 KB Output is correct
3 Correct 29 ms 5508 KB Output is correct
4 Correct 33 ms 5436 KB Output is correct
5 Correct 28 ms 5452 KB Output is correct
6 Correct 35 ms 5400 KB Output is correct
7 Correct 32 ms 5396 KB Output is correct
8 Correct 31 ms 5428 KB Output is correct
9 Correct 29 ms 5324 KB Output is correct
10 Correct 30 ms 5400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 3752 KB Output is correct
2 Correct 23 ms 3808 KB Output is correct
3 Correct 19 ms 3804 KB Output is correct
4 Correct 22 ms 3752 KB Output is correct
5 Correct 38 ms 5340 KB Output is correct
6 Correct 19 ms 3744 KB Output is correct
7 Correct 22 ms 3792 KB Output is correct
8 Correct 29 ms 5432 KB Output is correct
9 Correct 29 ms 5368 KB Output is correct
10 Correct 19 ms 3796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 236 ms 18380 KB Output is correct
2 Correct 234 ms 18168 KB Output is correct
3 Correct 227 ms 18256 KB Output is correct
4 Correct 202 ms 18080 KB Output is correct
5 Correct 226 ms 18228 KB Output is correct
6 Correct 236 ms 18364 KB Output is correct
7 Correct 261 ms 18480 KB Output is correct
8 Correct 235 ms 18260 KB Output is correct
9 Correct 216 ms 18468 KB Output is correct
10 Correct 243 ms 18472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1488 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 2 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1488 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 2 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1488 KB Output is correct
12 Correct 1 ms 1492 KB Output is correct
13 Correct 1 ms 1492 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1492 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 1 ms 1492 KB Output is correct
19 Correct 1 ms 1488 KB Output is correct
20 Correct 1 ms 1492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1488 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1620 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 2 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1488 KB Output is correct
12 Correct 1 ms 1492 KB Output is correct
13 Correct 1 ms 1492 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1492 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 1 ms 1492 KB Output is correct
19 Correct 1 ms 1488 KB Output is correct
20 Correct 1 ms 1492 KB Output is correct
21 Correct 2 ms 1620 KB Output is correct
22 Correct 1 ms 1636 KB Output is correct
23 Correct 2 ms 1520 KB Output is correct
24 Correct 2 ms 1620 KB Output is correct
25 Correct 1 ms 1628 KB Output is correct
26 Correct 2 ms 1620 KB Output is correct
27 Correct 2 ms 1524 KB Output is correct
28 Correct 2 ms 1620 KB Output is correct
29 Correct 2 ms 1620 KB Output is correct
30 Correct 3 ms 1632 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5376 KB Output is correct
2 Correct 37 ms 5392 KB Output is correct
3 Correct 29 ms 5508 KB Output is correct
4 Correct 33 ms 5436 KB Output is correct
5 Correct 28 ms 5452 KB Output is correct
6 Correct 35 ms 5400 KB Output is correct
7 Correct 32 ms 5396 KB Output is correct
8 Correct 31 ms 5428 KB Output is correct
9 Correct 29 ms 5324 KB Output is correct
10 Correct 30 ms 5400 KB Output is correct
11 Correct 18 ms 3752 KB Output is correct
12 Correct 23 ms 3808 KB Output is correct
13 Correct 19 ms 3804 KB Output is correct
14 Correct 22 ms 3752 KB Output is correct
15 Correct 38 ms 5340 KB Output is correct
16 Correct 19 ms 3744 KB Output is correct
17 Correct 22 ms 3792 KB Output is correct
18 Correct 29 ms 5432 KB Output is correct
19 Correct 29 ms 5368 KB Output is correct
20 Correct 19 ms 3796 KB Output is correct
21 Correct 236 ms 18380 KB Output is correct
22 Correct 234 ms 18168 KB Output is correct
23 Correct 227 ms 18256 KB Output is correct
24 Correct 202 ms 18080 KB Output is correct
25 Correct 226 ms 18228 KB Output is correct
26 Correct 236 ms 18364 KB Output is correct
27 Correct 261 ms 18480 KB Output is correct
28 Correct 235 ms 18260 KB Output is correct
29 Correct 216 ms 18468 KB Output is correct
30 Correct 243 ms 18472 KB Output is correct
31 Correct 1 ms 1488 KB Output is correct
32 Correct 1 ms 1492 KB Output is correct
33 Correct 1 ms 1492 KB Output is correct
34 Correct 1 ms 1492 KB Output is correct
35 Correct 1 ms 1492 KB Output is correct
36 Correct 1 ms 1492 KB Output is correct
37 Correct 1 ms 1620 KB Output is correct
38 Correct 1 ms 1492 KB Output is correct
39 Correct 2 ms 1492 KB Output is correct
40 Correct 1 ms 1492 KB Output is correct
41 Correct 1 ms 1488 KB Output is correct
42 Correct 1 ms 1492 KB Output is correct
43 Correct 1 ms 1492 KB Output is correct
44 Correct 1 ms 1492 KB Output is correct
45 Correct 1 ms 1492 KB Output is correct
46 Correct 1 ms 1492 KB Output is correct
47 Correct 1 ms 1492 KB Output is correct
48 Correct 1 ms 1492 KB Output is correct
49 Correct 1 ms 1488 KB Output is correct
50 Correct 1 ms 1492 KB Output is correct
51 Correct 2 ms 1620 KB Output is correct
52 Correct 1 ms 1636 KB Output is correct
53 Correct 2 ms 1520 KB Output is correct
54 Correct 2 ms 1620 KB Output is correct
55 Correct 1 ms 1628 KB Output is correct
56 Correct 2 ms 1620 KB Output is correct
57 Correct 2 ms 1524 KB Output is correct
58 Correct 2 ms 1620 KB Output is correct
59 Correct 2 ms 1620 KB Output is correct
60 Correct 3 ms 1632 KB Output is correct
61 Correct 143 ms 18044 KB Output is correct
62 Correct 141 ms 18520 KB Output is correct
63 Correct 209 ms 18000 KB Output is correct
64 Correct 149 ms 18396 KB Output is correct
65 Correct 176 ms 18420 KB Output is correct
66 Correct 189 ms 18352 KB Output is correct
67 Correct 202 ms 18048 KB Output is correct
68 Correct 257 ms 18444 KB Output is correct
69 Correct 205 ms 18108 KB Output is correct
70 Correct 237 ms 17980 KB Output is correct