Submission #868409

# Submission time Handle Problem Language Result Execution time Memory
868409 2023-10-31T12:27:10 Z mgl_diamond Candies (JOI18_candies) C++17
100 / 100
83 ms 14232 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
 
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"

void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  if (fopen(file".inp", "r")) {
    freopen(file".inp", "r", stdin);
    freopen(file".out", "w", stdout);
  }
}

const ll INF = 1e18;
const int N = 2e5+5;
ll n, a[N], l[N], r[N], del[N];
priority_queue<pair<ll, ll>> pq;

int main() {
  setIO();

  cin >> n;
  foru(i, 1, n) {
    cin >> a[i];
    pq.push({a[i], i});
    l[i] = i-1;
    r[i] = i+1;
  }
  a[0] = a[n+1] = -INF;

  ll ans = 0;
  foru(z, 1, (n+1) >> 1) {
    while (del[pq.top().se]) pq.pop();

    auto [val, i] = pq.top();
    pq.pop();

    ans += val;
    cout << ans << "\n";

    a[i] = a[l[i]] + a[r[i]] - a[i];
    pq.push({a[i], i});

    del[l[i]] = del[r[i]] = 1;
    r[l[l[i]]] = i;
    l[r[r[i]]] = i;
    l[i] = l[l[i]];
    r[i] = r[r[i]];
  }
}

Compilation message

candies.cpp: In function 'void setIO()':
candies.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |     freopen(file".inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen(file".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4668 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 2 ms 4696 KB Output is correct
15 Correct 2 ms 4576 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4732 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4668 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB Output is correct
2 Correct 1 ms 4700 KB Output is correct
3 Correct 1 ms 4700 KB Output is correct
4 Correct 1 ms 4700 KB Output is correct
5 Correct 1 ms 4700 KB Output is correct
6 Correct 1 ms 4700 KB Output is correct
7 Correct 1 ms 4700 KB Output is correct
8 Correct 1 ms 4700 KB Output is correct
9 Correct 1 ms 4668 KB Output is correct
10 Correct 1 ms 4700 KB Output is correct
11 Correct 1 ms 4700 KB Output is correct
12 Correct 1 ms 4700 KB Output is correct
13 Correct 1 ms 4700 KB Output is correct
14 Correct 2 ms 4696 KB Output is correct
15 Correct 2 ms 4576 KB Output is correct
16 Correct 1 ms 4700 KB Output is correct
17 Correct 1 ms 4700 KB Output is correct
18 Correct 1 ms 4732 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4668 KB Output is correct
21 Correct 72 ms 14232 KB Output is correct
22 Correct 74 ms 13240 KB Output is correct
23 Correct 83 ms 13252 KB Output is correct
24 Correct 62 ms 13444 KB Output is correct
25 Correct 60 ms 13244 KB Output is correct
26 Correct 55 ms 13864 KB Output is correct
27 Correct 68 ms 13376 KB Output is correct
28 Correct 60 ms 13424 KB Output is correct
29 Correct 61 ms 13252 KB Output is correct
30 Correct 63 ms 13304 KB Output is correct
31 Correct 70 ms 13420 KB Output is correct
32 Correct 71 ms 13288 KB Output is correct
33 Correct 63 ms 12988 KB Output is correct
34 Correct 66 ms 12988 KB Output is correct
35 Correct 63 ms 13244 KB Output is correct
36 Correct 70 ms 14016 KB Output is correct
37 Correct 82 ms 14208 KB Output is correct
38 Correct 69 ms 13328 KB Output is correct
39 Correct 54 ms 13588 KB Output is correct
40 Correct 56 ms 13184 KB Output is correct
41 Correct 60 ms 13508 KB Output is correct
42 Correct 69 ms 13244 KB Output is correct
43 Correct 62 ms 13252 KB Output is correct
44 Correct 62 ms 14100 KB Output is correct
45 Correct 62 ms 13968 KB Output is correct
46 Correct 62 ms 14016 KB Output is correct
47 Correct 65 ms 13376 KB Output is correct
48 Correct 66 ms 14072 KB Output is correct
49 Correct 69 ms 13596 KB Output is correct
50 Correct 71 ms 13980 KB Output is correct