Submission #1208137

#TimeUsernameProblemLanguageResultExecution timeMemory
1208137trandangquangCandies (JOI18_candies)C++20
100 / 100
60 ms8400 KiB
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define bit(s, i) (((s) >> (i)) & 1)
#define ii pair <int, int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll

template <class X, class Y>
    bool maximize(X &x, Y y) {
        if(x < y) {
            x = y;
            return true;
        }
        return false;
    }

template <class X, class Y>
    bool minimize(X &x, Y y) {
        if(x > y) {
            x = y;
            return true;
        }
        return false;
    }

void solve();
int32_t main() {
    #define task "test"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);

    int tc = 1; //cin >> tc;
    FOR(i, 1, tc){
        solve();
    }
}

const int N=2e5+5;
const ll INF=1e18;

int n,pre[N],nxt[N]; ll a[N]; bool del[N];
priority_queue<pair<ll,int>> pq;

void solve() {
    cin>>n;
    FOR(i,1,n) cin>>a[i];

    a[0]=a[n+1]=-INF;
    FOR(i,1,n) pre[i]=i-1, nxt[i]=i+1, pq.emplace(a[i],i);

    ll res=0;
    FOR(i,1,(n+1)/2){
        auto [val,id]=pq.top(); pq.pop();
        while(del[id]) id=pq.top().se, val=pq.top().fi, pq.pop();

        res+=val;
        a[id]=-a[id]+a[pre[id]]+a[nxt[id]];
        pq.emplace(a[id],id);
        del[pre[id]]=del[nxt[id]]=true;
        nxt[pre[pre[id]]]=id;
        pre[nxt[nxt[id]]]=id;
        pre[id]=pre[pre[id]];
        nxt[id]=nxt[nxt[id]];

        cout<<res<<'\n';
    }
}

Compilation message (stderr)

candies.cpp: In function 'int32_t main()':
candies.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   40 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...