Submission #990346

# Submission time Handle Problem Language Result Execution time Memory
990346 2024-05-30T09:04:29 Z Celebi_276 Candies (JOI18_candies) C++17
100 / 100
67 ms 9156 KB
#include <bits/stdc++.h>
using namespace std;

#define      FILE   "TEST"
#define    all(v)   v.begin(), v.end()
#define   rall(v)   v.rbegin(), v.rend()
#define        fi   first
#define        se   second

const int MaxN = 2E5 + 25;
int Prev[MaxN], Next[MaxN];
int n, id;

long long res, cur, a[MaxN];
bool used[MaxN];

void Erase (int id) {
    used[id] = 1;
    if (Prev[id] > 0)  Next[Prev[id]] = Next[id];
    if (Next[id] <= n) Prev[Next[id]] = Prev[id];
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    if (fopen(FILE".INP", "r")) {
        freopen(FILE".INP", "r", stdin);
        freopen(FILE".OUT", "w", stdout);
    }
    
    priority_queue <pair <long long, int>> Q;

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        Prev[i] = i - 1;
        Next[i] = i + 1;
        Q.emplace(a[i], i);
    }

    for (int i = 1; i <= (n + 1) / 2; i++) {
        while (Q.size() && used[Q.top().second]) Q.pop();

        tie(cur, id) = Q.top();
        Q.pop();

        res += cur;

        if (Prev[id] > 0 && Next[id] <= n) Q.emplace(a[id] = a[Prev[id]] + a[Next[id]] - cur, id);
        else Erase(id);

        if (Prev[id] > 0)  Erase(Prev[id]);
        if (Next[id] <= n) Erase(Next[id]);

        cout << res << "\n";
    }
    
    return 0;
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:28:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |         freopen(FILE".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   29 |         freopen(FILE".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2464 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2596 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2464 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2596 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 63 ms 8328 KB Output is correct
22 Correct 66 ms 8640 KB Output is correct
23 Correct 61 ms 8376 KB Output is correct
24 Correct 49 ms 9152 KB Output is correct
25 Correct 51 ms 8896 KB Output is correct
26 Correct 51 ms 8380 KB Output is correct
27 Correct 56 ms 8332 KB Output is correct
28 Correct 56 ms 8388 KB Output is correct
29 Correct 56 ms 8636 KB Output is correct
30 Correct 56 ms 8380 KB Output is correct
31 Correct 58 ms 8476 KB Output is correct
32 Correct 56 ms 8492 KB Output is correct
33 Correct 58 ms 9156 KB Output is correct
34 Correct 57 ms 8400 KB Output is correct
35 Correct 58 ms 8384 KB Output is correct
36 Correct 65 ms 8444 KB Output is correct
37 Correct 62 ms 8376 KB Output is correct
38 Correct 63 ms 8380 KB Output is correct
39 Correct 48 ms 8480 KB Output is correct
40 Correct 48 ms 8516 KB Output is correct
41 Correct 53 ms 8380 KB Output is correct
42 Correct 57 ms 8380 KB Output is correct
43 Correct 56 ms 8344 KB Output is correct
44 Correct 56 ms 8384 KB Output is correct
45 Correct 58 ms 8440 KB Output is correct
46 Correct 56 ms 8912 KB Output is correct
47 Correct 56 ms 8404 KB Output is correct
48 Correct 57 ms 8660 KB Output is correct
49 Correct 67 ms 8388 KB Output is correct
50 Correct 58 ms 8404 KB Output is correct