제출 #1103827

#제출 시각아이디문제언어결과실행 시간메모리
1103827hiensumiCandies (JOI18_candies)C++14
100 / 100
80 ms11204 KiB
#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;
}

컴파일 시 표준 에러 (stderr) 메시지

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...