답안 #998305

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998305 2024-06-13T15:07:49 Z LOLOLO Feast (NOI19_feast) C++17
100 / 100
151 ms 20880 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
 
#define           f     first
#define           s     second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (ll)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())
 
const int N = 3e5 + 10;
int p[N], sz[N], l[N], r[N], a[N];
ll val[N];
 
set <pair <ll, ll>> st;
 
int get(int x) {
    return p[x] ? get(p[x]) : x;
}
 
void unite(int a, int b) {
    a = get(a), b = get(b);
    if (a == b)
        return;
 
    if (sz[a] < sz[b])
        swap(a, b);
 
    sz[a] += sz[b];
    val[a] += val[b];
    p[b] = a;
    l[a] = min(l[a], l[b]);
    r[a] = max(r[a], r[b]);
}
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    //freopen("solution.inp", "r", stdin);
    //freopen("solution.ans", "w", stdout);
    int n, k;
    cin >> n >> k;
 
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        sz[i] = 1;
        l[i] = r[i] = i;
    }
 
    while (a[n] <= 0 && n)
        n--;
 
    if (n == 0) {
        cout << 0 << '\n';
        return 0;
    } 
 
    int ft = 1;
    while (a[ft] <= 0)
        ft++;
 
    for (int j = ft; j <= n; j++) 
        a[j - ft + 1] = a[j];

    n -= (ft - 1);

    for (int i = 1; i <= n; i++) {
        val[i] = a[i];
    }
 
    for (int i = 1; i < n; i++) 
        if ((a[i] >= 0) == (a[i + 1] >= 0))
            unite(i, i + 1);
 
    for (int i = 1; i <= n; i++) 
        if (get(i) == i) 
            st.insert({abs(val[get(i)]), i});
 
    int ss = 1;
    while (sz(st) > k * 2) {
        auto it = *st.begin(); 
        st.erase(it);
        int p = it.s;
 
        if (l[p] > ss) {
            int x = get(l[p] - 1);
            st.erase({abs(val[x]), x});
            unite(p, x);
        }
 
        if (r[p] < n) {
            int x = get(r[p] + 1);
            st.erase({abs(val[x]), x});
            unite(p, x);
        }
 
        p = get(p);
        st.insert({abs(val[p]), p});
 
        while (val[get(n)] < 0) {
            st.erase({abs(val[get(n)]), get(n)});
            n = l[get(n)] - 1;
        }
 
        while (val[get(ss)] < 0) {
            st.erase({abs(val[get(ss)]), get(ss)});
            ss = r[get(ss)] + 1;
        }
    }
 
    ll ans = 0;
    for (auto x : st)
        ans += max((ll)0, val[x.s]);

    cout << ans << '\n';
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 10324 KB Output is correct
2 Correct 23 ms 10324 KB Output is correct
3 Correct 23 ms 10312 KB Output is correct
4 Correct 23 ms 10320 KB Output is correct
5 Correct 24 ms 10324 KB Output is correct
6 Correct 22 ms 10320 KB Output is correct
7 Correct 23 ms 10320 KB Output is correct
8 Correct 23 ms 10324 KB Output is correct
9 Correct 24 ms 10324 KB Output is correct
10 Correct 23 ms 10376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 9304 KB Output is correct
2 Correct 15 ms 9396 KB Output is correct
3 Correct 15 ms 9308 KB Output is correct
4 Correct 15 ms 9152 KB Output is correct
5 Correct 23 ms 10320 KB Output is correct
6 Correct 15 ms 9308 KB Output is correct
7 Correct 22 ms 9360 KB Output is correct
8 Correct 23 ms 10300 KB Output is correct
9 Correct 23 ms 10320 KB Output is correct
10 Correct 16 ms 9400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 19836 KB Output is correct
2 Correct 138 ms 19544 KB Output is correct
3 Correct 142 ms 19664 KB Output is correct
4 Correct 138 ms 19540 KB Output is correct
5 Correct 138 ms 19636 KB Output is correct
6 Correct 151 ms 19792 KB Output is correct
7 Correct 139 ms 19792 KB Output is correct
8 Correct 135 ms 19792 KB Output is correct
9 Correct 140 ms 19796 KB Output is correct
10 Correct 143 ms 19876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 0 ms 4560 KB Output is correct
5 Correct 0 ms 4564 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 0 ms 4560 KB Output is correct
5 Correct 0 ms 4564 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4440 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4444 KB Output is correct
4 Correct 0 ms 4560 KB Output is correct
5 Correct 0 ms 4564 KB Output is correct
6 Correct 1 ms 4440 KB Output is correct
7 Correct 1 ms 4444 KB Output is correct
8 Correct 0 ms 4444 KB Output is correct
9 Correct 1 ms 4444 KB Output is correct
10 Correct 1 ms 4444 KB Output is correct
11 Correct 1 ms 4444 KB Output is correct
12 Correct 1 ms 4444 KB Output is correct
13 Correct 1 ms 4444 KB Output is correct
14 Correct 1 ms 4444 KB Output is correct
15 Correct 1 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 1 ms 4444 KB Output is correct
21 Correct 1 ms 4444 KB Output is correct
22 Correct 1 ms 4444 KB Output is correct
23 Correct 1 ms 4444 KB Output is correct
24 Correct 1 ms 4680 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 1 ms 4444 KB Output is correct
27 Correct 1 ms 4444 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 1 ms 4444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 10324 KB Output is correct
2 Correct 23 ms 10324 KB Output is correct
3 Correct 23 ms 10312 KB Output is correct
4 Correct 23 ms 10320 KB Output is correct
5 Correct 24 ms 10324 KB Output is correct
6 Correct 22 ms 10320 KB Output is correct
7 Correct 23 ms 10320 KB Output is correct
8 Correct 23 ms 10324 KB Output is correct
9 Correct 24 ms 10324 KB Output is correct
10 Correct 23 ms 10376 KB Output is correct
11 Correct 15 ms 9304 KB Output is correct
12 Correct 15 ms 9396 KB Output is correct
13 Correct 15 ms 9308 KB Output is correct
14 Correct 15 ms 9152 KB Output is correct
15 Correct 23 ms 10320 KB Output is correct
16 Correct 15 ms 9308 KB Output is correct
17 Correct 22 ms 9360 KB Output is correct
18 Correct 23 ms 10300 KB Output is correct
19 Correct 23 ms 10320 KB Output is correct
20 Correct 16 ms 9400 KB Output is correct
21 Correct 142 ms 19836 KB Output is correct
22 Correct 138 ms 19544 KB Output is correct
23 Correct 142 ms 19664 KB Output is correct
24 Correct 138 ms 19540 KB Output is correct
25 Correct 138 ms 19636 KB Output is correct
26 Correct 151 ms 19792 KB Output is correct
27 Correct 139 ms 19792 KB Output is correct
28 Correct 135 ms 19792 KB Output is correct
29 Correct 140 ms 19796 KB Output is correct
30 Correct 143 ms 19876 KB Output is correct
31 Correct 1 ms 4440 KB Output is correct
32 Correct 1 ms 4444 KB Output is correct
33 Correct 1 ms 4444 KB Output is correct
34 Correct 0 ms 4560 KB Output is correct
35 Correct 0 ms 4564 KB Output is correct
36 Correct 1 ms 4440 KB Output is correct
37 Correct 1 ms 4444 KB Output is correct
38 Correct 0 ms 4444 KB Output is correct
39 Correct 1 ms 4444 KB Output is correct
40 Correct 1 ms 4444 KB Output is correct
41 Correct 1 ms 4444 KB Output is correct
42 Correct 1 ms 4444 KB Output is correct
43 Correct 1 ms 4444 KB Output is correct
44 Correct 1 ms 4444 KB Output is correct
45 Correct 1 ms 4444 KB Output is correct
46 Correct 1 ms 4444 KB Output is correct
47 Correct 1 ms 4444 KB Output is correct
48 Correct 1 ms 4444 KB Output is correct
49 Correct 1 ms 4444 KB Output is correct
50 Correct 1 ms 4444 KB Output is correct
51 Correct 1 ms 4444 KB Output is correct
52 Correct 1 ms 4444 KB Output is correct
53 Correct 1 ms 4444 KB Output is correct
54 Correct 1 ms 4680 KB Output is correct
55 Correct 1 ms 4444 KB Output is correct
56 Correct 1 ms 4444 KB Output is correct
57 Correct 1 ms 4444 KB Output is correct
58 Correct 1 ms 4444 KB Output is correct
59 Correct 1 ms 4444 KB Output is correct
60 Correct 1 ms 4444 KB Output is correct
61 Correct 98 ms 20564 KB Output is correct
62 Correct 78 ms 20880 KB Output is correct
63 Correct 133 ms 20560 KB Output is correct
64 Correct 97 ms 20820 KB Output is correct
65 Correct 118 ms 20820 KB Output is correct
66 Correct 125 ms 20740 KB Output is correct
67 Correct 107 ms 20560 KB Output is correct
68 Correct 138 ms 20816 KB Output is correct
69 Correct 133 ms 20664 KB Output is correct
70 Correct 140 ms 20344 KB Output is correct