Submission #1124368

#TimeUsernameProblemLanguageResultExecution timeMemory
1124368pndhpndhCandies (JOI18_candies)C++20
100 / 100
88 ms10672 KiB
#include<bits/stdc++.h> #define int long long #define pb push_back #define task "task" using namespace std; const int inf = 1e18; const int mx = 2e5 + 5; int n, m; int a[mx]; int pre[mx], nxt[mx]; bool vs[mx]; int res[mx]; void read(){ cin >> n; for (int i = 1; i <= n; i++){ cin >> a[i]; pre[i] = i - 1; nxt[i] = i + 1; } a[0] = a[n + 1] = -inf; } void solve(){ priority_queue<pair<int,int>> pq; for (int i = 1; i <= n; i++){ pq.push({a[i], i}); } int cnt = 0; int cur = 0; while (!pq.empty() && cnt <= (n + 1)/2){ int val = pq.top().first, id = pq.top().second; pq.pop(); if (vs[id]) continue; cur += val; cnt++; res[cnt] = cur; a[id] = a[pre[id]] + a[nxt[id]] - a[id]; vs[nxt[id]] = vs[pre[id]] = true; pre[id] = pre[pre[id]]; nxt[id] = nxt[nxt[id]]; nxt[pre[id]] = pre[nxt[id]] = id; pq.push({a[id], id}); } for (int i = 1; i < cnt; i++){ cout << res[i] << '\n'; } } signed main(){ cin.tie(0)->sync_with_stdio(0); if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); } read(); solve(); }

Compilation message (stderr)

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