Submission #1103827

# Submission time Handle Problem Language Result Execution time Memory
1103827 2024-10-21T22:35:29 Z hiensumi Candies (JOI18_candies) C++14
100 / 100
80 ms 11204 KB
#include <bits/stdc++.h>
using namespace std;
#define fod(i,a,b) for(int i = (a); i <= (b); i++)
#define fok(i,a,b) for(int i = (a); i >= (b); i--)
#define ll long long
#define el '\n'
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define ve vector
#define vi vector<int>
#define vll vector<ll>
#define mask(i) (1LL<<(i))
#define BIT(msk, i) (msk>>(i)&1LL)
template <class T> bool mini(T &a, T b){ return (a > (b)) ? a = (b), 1 : 0; }
template <class T> bool maxi(T &a, T b){ return (a < (b)) ? a = (b), 1 : 0; }
const int base = mask(20) + 5;
#define name "candies"

const int N = 2e5;
int n;

priority_queue <pair<ll,int>> pq;

ll a[N + 5];
int pre[N + 5];
int nxt[N + 5];

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    if(fopen(name".inp", "r")){
        freopen(name".inp", "r", stdin);
        freopen(name".out", "w", stdout);
    }

    cin >> n;

    fod(i,1,n){
        cin >> a[i];
        if(i > 1) pre[i] = i - 1;
        if(i < n) nxt[i] = i + 1;
        pq.emplace(a[i], i);
    }

    ll res = 0;

    fod(k,1,(n+1)/2){
        while(!pq.empty() and pq.top().fi != a[pq.top().se]) pq.pop();

        res += pq.top().fi;

        cout << res << el;

        int i = pq.top().se;

        int l = pre[i], r = nxt[i];

        nxt[pre[l]] = i;
        pre[nxt[r]] = i;
        pre[i] = pre[l];
        nxt[i] = nxt[r];

        if(l and r) a[i] = max((ll) -1e18, a[l] + a[r] - a[i]);
        else a[i] = -1e18;

        a[l] = a[r] = -1e18;
        pq.emplace(a[i], i);
    }

    return 0;
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:34:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   34 |         freopen(name".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   35 |         freopen(name".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 2 ms 2620 KB Output is correct
4 Correct 2 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 2 ms 2384 KB Output is correct
9 Correct 2 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 2 ms 2384 KB Output is correct
12 Correct 2 ms 2384 KB Output is correct
13 Correct 2 ms 2384 KB Output is correct
14 Correct 2 ms 2384 KB Output is correct
15 Correct 2 ms 2384 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 2 ms 2384 KB Output is correct
19 Correct 2 ms 2384 KB Output is correct
20 Correct 2 ms 2384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2384 KB Output is correct
2 Correct 1 ms 2384 KB Output is correct
3 Correct 2 ms 2620 KB Output is correct
4 Correct 2 ms 2384 KB Output is correct
5 Correct 1 ms 2384 KB Output is correct
6 Correct 1 ms 2384 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 2 ms 2384 KB Output is correct
9 Correct 2 ms 2384 KB Output is correct
10 Correct 1 ms 2384 KB Output is correct
11 Correct 2 ms 2384 KB Output is correct
12 Correct 2 ms 2384 KB Output is correct
13 Correct 2 ms 2384 KB Output is correct
14 Correct 2 ms 2384 KB Output is correct
15 Correct 2 ms 2384 KB Output is correct
16 Correct 1 ms 2384 KB Output is correct
17 Correct 1 ms 2384 KB Output is correct
18 Correct 2 ms 2384 KB Output is correct
19 Correct 2 ms 2384 KB Output is correct
20 Correct 2 ms 2384 KB Output is correct
21 Correct 74 ms 11100 KB Output is correct
22 Correct 71 ms 11188 KB Output is correct
23 Correct 68 ms 11192 KB Output is correct
24 Correct 53 ms 10948 KB Output is correct
25 Correct 55 ms 10940 KB Output is correct
26 Correct 53 ms 10936 KB Output is correct
27 Correct 60 ms 11152 KB Output is correct
28 Correct 67 ms 11192 KB Output is correct
29 Correct 64 ms 11092 KB Output is correct
30 Correct 61 ms 11204 KB Output is correct
31 Correct 70 ms 11068 KB Output is correct
32 Correct 65 ms 11196 KB Output is correct
33 Correct 66 ms 10940 KB Output is correct
34 Correct 66 ms 10940 KB Output is correct
35 Correct 62 ms 10940 KB Output is correct
36 Correct 73 ms 11196 KB Output is correct
37 Correct 80 ms 11096 KB Output is correct
38 Correct 68 ms 11132 KB Output is correct
39 Correct 55 ms 10940 KB Output is correct
40 Correct 55 ms 10940 KB Output is correct
41 Correct 60 ms 10940 KB Output is correct
42 Correct 62 ms 11192 KB Output is correct
43 Correct 63 ms 11196 KB Output is correct
44 Correct 64 ms 11120 KB Output is correct
45 Correct 65 ms 11196 KB Output is correct
46 Correct 59 ms 11196 KB Output is correct
47 Correct 59 ms 11196 KB Output is correct
48 Correct 60 ms 10940 KB Output is correct
49 Correct 60 ms 10936 KB Output is correct
50 Correct 60 ms 10948 KB Output is correct