답안 #963739

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
963739 2024-04-15T14:56:31 Z Cookie Candies (JOI18_candies) C++14
100 / 100
120 ms 16328 KB
#include<bits/stdc++.h>
#include<fstream>
using namespace std;
#define sz(a) (int)a.size()
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
#define mpp make_pair
const ld PI = 3.14159265359, prec = 1e-9;;
//using u128 = __uint128_t;
//const int x[4] = {1, 0, -1, 0};
//const int y[4] = {0, -1, 0, 1};
const ll mod = 1e9 + 7, pr = 31;
const int mxn = 5e5 + 5, mxq = 1e5 + 5, sq = 500, mxv = 5e4 + 1;
//const int base = (1 <<18);
const ll inf = 1e9 + 5, neg = -69420, inf2 = 1e14;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// have fun!
//JOIIIIIIIIIIIIIII
struct ch{
    ll v, l, r;
};
struct cmp{
    bool operator ()(const ch &a, const ch &b){
        return(a.v < b.v);
    }
};
int n;
ll a[mxn + 1];
int nxt[mxn + 1], last[mxn + 1];
bool vis[mxn + 1];
void solve(){
    cin >> n;
    priority_queue<ch, vt<ch>, cmp>pq;
    for(int i = 1; i <= n; i++){
        cin >> a[i]; last[i] = i - 1; nxt[i] = i + 1; pq.push({a[i], i, i + 1});
    }
    last[n + 1] = n;  nxt[n + 1] = n + 2;
    int take = 0;
    ll ans = 0;
    while(!pq.empty()){
        auto [val, l, r] = pq.top(); pq.pop();
        if(vis[l] || vis[r])continue;
        ans += val; take++;
        cout << ans << "\n";
        if(take == (n + 1) / 2){
            return;
        }
        vis[l] = vis[r] = 1;
        int u = last[l], v = nxt[r];
        
        if(u >= 1 && v <= n + 1){
            nxt[u] = v; last[v] = u; a[u] += a[r] - a[l];
            pq.push({a[u], u, v});
        }
    }
}
signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("DATA.inp", "r", stdin);
    //freopen("DATA.out", "w", stdout);
    int tt; tt = 1;
    while(tt--){
        solve();
 
    }
    return(0);
}

Compilation message

candies.cpp: In function 'void solve()':
candies.cpp:52:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   52 |         auto [val, l, r] = pq.top(); pq.pop();
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6744 KB Output is correct
9 Correct 1 ms 6744 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 2 ms 6748 KB Output is correct
12 Correct 2 ms 6748 KB Output is correct
13 Correct 2 ms 6744 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6748 KB Output is correct
18 Correct 2 ms 6744 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 6748 KB Output is correct
2 Correct 2 ms 6748 KB Output is correct
3 Correct 2 ms 6748 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
5 Correct 2 ms 6748 KB Output is correct
6 Correct 2 ms 6744 KB Output is correct
7 Correct 2 ms 6748 KB Output is correct
8 Correct 2 ms 6744 KB Output is correct
9 Correct 1 ms 6744 KB Output is correct
10 Correct 2 ms 6744 KB Output is correct
11 Correct 2 ms 6748 KB Output is correct
12 Correct 2 ms 6748 KB Output is correct
13 Correct 2 ms 6744 KB Output is correct
14 Correct 2 ms 6748 KB Output is correct
15 Correct 2 ms 6748 KB Output is correct
16 Correct 2 ms 6748 KB Output is correct
17 Correct 2 ms 6748 KB Output is correct
18 Correct 2 ms 6744 KB Output is correct
19 Correct 2 ms 6744 KB Output is correct
20 Correct 2 ms 6748 KB Output is correct
21 Correct 83 ms 16076 KB Output is correct
22 Correct 87 ms 15740 KB Output is correct
23 Correct 83 ms 15704 KB Output is correct
24 Correct 63 ms 15308 KB Output is correct
25 Correct 64 ms 15496 KB Output is correct
26 Correct 67 ms 15444 KB Output is correct
27 Correct 58 ms 16052 KB Output is correct
28 Correct 76 ms 15540 KB Output is correct
29 Correct 61 ms 16328 KB Output is correct
30 Correct 59 ms 15536 KB Output is correct
31 Correct 62 ms 16076 KB Output is correct
32 Correct 59 ms 16068 KB Output is correct
33 Correct 69 ms 15820 KB Output is correct
34 Correct 63 ms 16056 KB Output is correct
35 Correct 69 ms 16312 KB Output is correct
36 Correct 89 ms 15944 KB Output is correct
37 Correct 120 ms 15612 KB Output is correct
38 Correct 81 ms 15592 KB Output is correct
39 Correct 59 ms 15340 KB Output is correct
40 Correct 73 ms 15560 KB Output is correct
41 Correct 77 ms 15492 KB Output is correct
42 Correct 72 ms 15988 KB Output is correct
43 Correct 59 ms 16076 KB Output is correct
44 Correct 60 ms 16076 KB Output is correct
45 Correct 64 ms 15420 KB Output is correct
46 Correct 67 ms 15640 KB Output is correct
47 Correct 57 ms 15816 KB Output is correct
48 Correct 63 ms 15044 KB Output is correct
49 Correct 75 ms 16064 KB Output is correct
50 Correct 71 ms 15948 KB Output is correct