제출 #1370818

#제출 시각아이디문제언어결과실행 시간메모리
1370818dungmahiruCandies (JOI18_candies)C++20
100 / 100
62 ms8520 KiB
#include <bits/stdc++.h>
using namespace std;
#define ShiinaMahiru signed main()
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

        bool __MEMORY_BEGIN__;

typedef long long           ll;
// #define int                 ll
typedef unsigned long long  ull;
typedef long double         ld;
typedef pair<int, int>      pii;
typedef pair<ll, ll>        pll;

#define el                  "\n"
#define gcd                 __gcd
#define lcm(a, b)           a / gcd(a,b) * b
#define get_bit(mask, i)    ((mask >> (i)) & 1)
#define set_bit(mask, i)    (mask | (1LL<<(i)))
#define low_bit(mask)       ((mask) & (-(mask)))
#define __builtin_popcount  __builtin_popcountll
#define pb                  push_back
#define emp                 emplace
#define emb                 emplace_back
#define emf                 emplace_front
#define mpair               make_pair
#define fi                  first
#define se                  second
#define rounds(n)           setprecision(n) << fixed
#define ALL(x)              x.begin(), x.end()

const int INF       = 0x3f3f3f3f;
const ll LLINF      = (ll)1e18+3;
const int MAXSIZE   = (int)2e5+3;
const ll MOD        = (int)1e9+7;
const string NAME   = "candies";

mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll L, ll R) { assert(L <= R); return L + rd() % (R - L + 1); }

template <class T> inline bool minimize(T &x, T y){ if (x > y){x = y; return true;} return false; }
template <class T> inline bool maximize(T &x, T y){ if (x < y){x = y; return true;} return false; }
inline ll Add_MOD(ll A, const ll& B) { A += B; if (A >= MOD) A -= MOD; return A; }
inline ll Sub_MOD(ll A, const ll& B) { A -= B; if (A < 0) A += MOD; return A; }

int N;
ll A[MAXSIZE];

void INPUT()
{
    cin >> N;
    for (int i = 1; i <= N; ++i){
        cin >> A[i];
    }
}

bool used[MAXSIZE];
int L[MAXSIZE], R[MAXSIZE];

void SOLVE()
{  
    A[0] = -LLINF;
    A[N+1] = -LLINF;
    priority_queue<pair<ll, int>> pq;
    for (int i = 1; i <= N; ++i){
        pq.push({A[i], i});
        L[i] = i-1;
        R[i] = i+1;
    }

    ll Res = 0;
    for (int j = 1; j <= (N+1)/2; ++j){
        while (!pq.empty() && used[pq.top().se]) pq.pop();
        ll v = pq.top().fi;
        int x = pq.top().se;
        pq.pop();
        // cerr << x << el;
        Res += v;

        ll new_v = A[L[x]] + A[R[x]] - A[x];
        A[x] = new_v;
        
        int lf = L[x], ri = R[x];

        used[lf] = used[ri] = true;
        L[x] = L[lf];
        R[x] = R[ri];
        R[L[x]] = L[R[x]] = x;
        pq.push({new_v, x});

        cout << Res << el;
    }
}

        bool __MEMORY_END__;

void report_static_memory_usage(void)
{
    size_t diff = (char*)&__MEMORY_END__ - (char*)&__MEMORY_BEGIN__;
    long double mb = diff / (1024.0 * 1024.0);
    cerr << rounds(9) << "Static memory usage: " << mb << " MB" << el;
}

ShiinaMahiru
{
    FAST_IO;
    clock_t start = clock();
    if (fopen("test.inp", "r")){
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }
    else if(fopen((NAME + ".inp").c_str(), "r")){
        freopen((NAME+".inp").c_str(), "r", stdin);
        freopen((NAME+".out").c_str(), "w", stdout);
    }
    int _t = 1;
    // cin >> _t;
    while (_t--){
        INPUT();
        SOLVE();
    }

    cerr << el << "------------------STATISTICS------------------" << el;
    report_static_memory_usage();
    cerr << "Time elapsed: " << clock() - start << "ms";
    cerr << el << "----------------------------------------------" << el;
    return 0;
}
/// Solution by Dung Vu - Informatics K36 CTN. Solve in 00h00 - 01/01/2026

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

candies.cpp: In function 'int main()':
candies.cpp:109:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:110:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  110 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen((NAME+".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |         freopen((NAME+".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…