Submission #932983

# Submission time Handle Problem Language Result Execution time Memory
932983 2024-02-24T16:58:23 Z Blistering_Barnacles Candies (JOI18_candies) C++17
100 / 100
263 ms 17256 KB
//Billions of bilious blue blistering barnacles in a thundering typhoon!
//Yesterday is history, tomorrow is a mystery, today is a gift of God, which is why we call it the present.
#include<bits/stdc++.h>
#define F first 
#define S second
#define lx node * 2
#define rx node * 2 + 1
#define md ((s + e) / 2)
#define mid ((l + r) / 2)
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
ll pref[200005];
int a[200005], L[200005], R[200005], n;
set < pair < int, int > > se;
set < pair < ll, pair < int, int > > > Se;
void add(int l, int r){
    if(l == 1 || r == n)return;
    Se.insert({pref[r + 1] - (l <= 2 ? 0 : pref[l - 3]) - ((pref[r] - (l <= 2 ? 0 : pref[l - 2]))), {l, r}});
}
void remove(int l, int r){
    if(l == 1 || r == n)return;
    Se.erase({pref[r + 1] - (l <= 2 ? 0 : pref[l - 3]) - ((pref[r] - (l <= 2 ? 0 : pref[l - 2]))), {l, r}});
}
void solve(){
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        se.insert({a[i], i});
        pref[i] = (i > 1 ? pref[i - 2] : 0) + a[i];
    }
    ll ans = 0;
    for(int i = 1; i <= (n + 1) / 2; i++){
        ll val1 = (se.empty() ? (ll)-1e18 : (*se.rbegin()).F);
        ll val2 = (Se.empty() ? (ll)-1e18 : (*Se.rbegin()).F);
        if(val1 > val2){
            ans += val1;
            int j = (*se.rbegin()).S;
            se.erase({a[j], j});
            se.erase({a[j - 1], j - 1});
            se.erase({a[j + 1], j + 1});
            int l = j, r = j;
            if(j > 2 && L[j - 2]){
                remove(L[j - 2], j - 2);
                l = L[j - 2];
            }
            if(j < n - 1 && R[j + 2]){
                remove(j + 2, R[j + 2]);
                r = R[j + 2];
            }
            L[r] = l, R[l] = r;
            add(l, r);
        }
        else{
            ans += val2;
            auto [l, r] = (*Se.rbegin()).S;
            remove(l, r);
            if(l == 1)l++;
            else l--;
            if(r == n)r--;
            else r++;
            if(l > 2 && L[l - 2]){
                remove(L[l - 2], l - 2);
                l = L[l - 2];
            }
            if(r < n - 1 && R[r + 2]){
                remove(r + 2, R[r + 2]);
                r = R[r + 2];
            }
            L[r] = l, R[l] = r;
            se.erase({a[l - 1], l - 1});
            se.erase({a[r + 1], r + 1});
            add(l, r);
        }
        cout << ans << "\n";
    }
}
int main(){
    ios::sync_with_stdio(0), cin.tie(0); 
    int tt = 1; 
    //cin >> tt; 
    while(tt--){
        solve(); 
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2640 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2632 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2528 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2652 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 2 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2640 KB Output is correct
8 Correct 1 ms 2652 KB Output is correct
9 Correct 1 ms 2648 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2632 KB Output is correct
13 Correct 2 ms 2636 KB Output is correct
14 Correct 2 ms 2528 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 1 ms 2652 KB Output is correct
17 Correct 1 ms 2652 KB Output is correct
18 Correct 1 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2648 KB Output is correct
21 Correct 241 ms 17256 KB Output is correct
22 Correct 233 ms 17236 KB Output is correct
23 Correct 263 ms 17228 KB Output is correct
24 Correct 108 ms 16984 KB Output is correct
25 Correct 119 ms 17048 KB Output is correct
26 Correct 115 ms 17040 KB Output is correct
27 Correct 127 ms 17240 KB Output is correct
28 Correct 118 ms 17108 KB Output is correct
29 Correct 120 ms 17160 KB Output is correct
30 Correct 118 ms 17236 KB Output is correct
31 Correct 132 ms 17056 KB Output is correct
32 Correct 144 ms 17108 KB Output is correct
33 Correct 169 ms 17036 KB Output is correct
34 Correct 172 ms 16888 KB Output is correct
35 Correct 164 ms 17240 KB Output is correct
36 Correct 199 ms 17232 KB Output is correct
37 Correct 208 ms 17236 KB Output is correct
38 Correct 238 ms 17256 KB Output is correct
39 Correct 98 ms 16872 KB Output is correct
40 Correct 97 ms 17064 KB Output is correct
41 Correct 105 ms 17136 KB Output is correct
42 Correct 124 ms 17236 KB Output is correct
43 Correct 117 ms 17236 KB Output is correct
44 Correct 121 ms 17160 KB Output is correct
45 Correct 135 ms 17048 KB Output is correct
46 Correct 122 ms 17232 KB Output is correct
47 Correct 132 ms 17232 KB Output is correct
48 Correct 178 ms 17052 KB Output is correct
49 Correct 177 ms 16976 KB Output is correct
50 Correct 173 ms 16916 KB Output is correct