Submission #676561

# Submission time Handle Problem Language Result Execution time Memory
676561 2022-12-31T09:13:55 Z Do_you_copy Candies (JOI18_candies) C++17
100 / 100
140 ms 7928 KB
//Then
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(0); cin.tie(0);

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair <int, int>;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 5e5 + 1;
//const int Mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f3f3f3f3f;
int n;
ll a[maxN];
bool invalid[maxN];
int nxt[maxN];
int pre[maxN];
struct TPQ{
    int i;
    bool operator < (const TPQ &other) const{
        return a[i] < a[other.i];
    }
};

priority_queue <TPQ> PQ;

void Init(){
    cin >> n;
    for (int i = 1; i <= n; ++i){
        cin >> a[i];
        PQ.push({i});
    }
    for (int i = 0; i <= n + 1; ++i){
        pre[i] = i - 1;
        nxt[i] = i + 1;
    }
    a[0] = a[n + 1] = -inf;
    nxt[n] = 0;
    ll ans = 0;
    int k = (n + 1) / 2;
    for (int _ = 1; _ <= k; ++_){
        while (!PQ.empty() && invalid[PQ.top().i]){
            PQ.pop();
        }
        if (PQ.empty()) break;
        int i = PQ.top().i;
        invalid[i] = 1;
        invalid[nxt[i]] = 1;
        invalid[pre[i]] = 1;
        ans += a[i];
        cout << ans << "\n";
        ++n;
        a[n] = a[nxt[i]] + a[pre[i]] - a[i];
        nxt[n] = nxt[nxt[i]];
        pre[n] = pre[pre[i]];
        nxt[pre[pre[i]]] = n;
        pre[nxt[nxt[i]]] = n;
        PQ.push({n});
    }
}

#define debug
#define taskname "test"
signed main(){
    faster
    if (fopen(taskname".inp", "r")){
        freopen(taskname".inp", "r", stdin);
        freopen(taskname".out", "w", stdout);
    }
    int tt = 1;
    //cin >> tt;
    while (tt--){
        Init();
    }
    if (fopen("timeout.txt", "r")){
        ofstream timeout("timeout.txt");
        timeout << signed(double(clock()) / CLOCKS_PER_SEC * 1000);
        timeout.close();
        #ifndef debug
        cerr << "Time elapsed: " << signed(double(clock()) / CLOCKS_PER_SEC * 1000) << "ms\n";
        #endif // debug
    }
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen(taskname".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
candies.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |         freopen(taskname".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 135 ms 7692 KB Output is correct
22 Correct 132 ms 7768 KB Output is correct
23 Correct 124 ms 7752 KB Output is correct
24 Correct 81 ms 7756 KB Output is correct
25 Correct 89 ms 7872 KB Output is correct
26 Correct 78 ms 7752 KB Output is correct
27 Correct 73 ms 7856 KB Output is correct
28 Correct 74 ms 7908 KB Output is correct
29 Correct 74 ms 7768 KB Output is correct
30 Correct 75 ms 7852 KB Output is correct
31 Correct 75 ms 7776 KB Output is correct
32 Correct 76 ms 7748 KB Output is correct
33 Correct 90 ms 7864 KB Output is correct
34 Correct 88 ms 7880 KB Output is correct
35 Correct 94 ms 7824 KB Output is correct
36 Correct 138 ms 7928 KB Output is correct
37 Correct 140 ms 7924 KB Output is correct
38 Correct 131 ms 7872 KB Output is correct
39 Correct 80 ms 7772 KB Output is correct
40 Correct 78 ms 7836 KB Output is correct
41 Correct 82 ms 7888 KB Output is correct
42 Correct 75 ms 7764 KB Output is correct
43 Correct 74 ms 7756 KB Output is correct
44 Correct 73 ms 7796 KB Output is correct
45 Correct 85 ms 7776 KB Output is correct
46 Correct 76 ms 7768 KB Output is correct
47 Correct 80 ms 7820 KB Output is correct
48 Correct 88 ms 7756 KB Output is correct
49 Correct 87 ms 7748 KB Output is correct
50 Correct 93 ms 7780 KB Output is correct