Submission #202256

#TimeUsernameProblemLanguageResultExecution timeMemory
202256quocnguyen1012Candies (JOI18_candies)C++14
100 / 100
141 ms10288 KiB
#include <bits/stdc++.h>
using namespace std;
 
const long long INF = 1e18;
 
int n;
long long a[200005];
int Left[200005], Right[200005];
priority_queue <pair <long long, int> > pq;
 
int main()
{
    scanf("%d", &n);
 
    for(int i = 1; i <= n ; ++i){
        scanf("%lld", &a[i]);
        Left[i] = i - 1; Right[i] = i + 1;
        pq.push({a[i], i});
    }
 
    a[0] = a[n + 1] = -INF;
    long long Sol = 0;
    for(int i = 1; i <= (n + 1) / 2 ; ++i){
        while(!pq.empty() && a[pq.top().second] != pq.top().first) pq.pop();
        int pos = pq.top().second;
        pq.pop();
        Sol += a[pos];
        printf("%lld\n", Sol);
 
        int L = Left[pos], R = Right[pos];
        Left[pos] = Left[L]; Right[pos] = Right[R];
        Right[Left[L]] = pos; Left[Right[R]] = pos;
 
        a[pos] = max(a[L] + a[R] - a[pos], -INF);
        a[L] = a[R] = -INF;
        pq.push({a[pos], pos});
    }
 
    return 0;
}

Compilation message (stderr)

candies.cpp: In function 'int main()':
candies.cpp:13:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
candies.cpp:16:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld", &a[i]);
         ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...