답안 #932806

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
932806 2024-02-24T09:26:29 Z Wael Candies (JOI18_candies) C++17
100 / 100
279 ms 35396 KB
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define log(x) (63^__builtin_clzll(x))
#define endl '\n'
#define int long long
typedef long long ll;
int const M = 1e6 + 10 , lg = 23 , mod[] = {(ll)1e9 + 7 , 100000643} , block = 700;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
int dx[] = {1 , -1 , 0 , 0} , dy[] = {0 , 0 , 1 , -1};
int s[M] , pref[M] , l[M] , r[M] , one[M];
set<pair<ll , ll>>blocks;

void eraseBlock(int L , int R) {

    int value = (pref[R + 1] - (L - 3 >= 1 ? pref[L - 3] : 0)) - (pref[R] - (L - 2 >= 1 ? pref[L - 2] : 0));
    blocks.erase({value , L});

}

void insertBlock(int L , int R) {

    one[L] = one[R] = 1;
    l[R] = L , r[L] = R;
    int value = (pref[R + 1] - (L - 3 >= 1 ? pref[L - 3] : 0)) - (pref[R] - (L - 2 >= 1 ? pref[L - 2] : 0));
    blocks.insert({value , L});

}

void merge(int l1 , int r1 , int l2 , int r2) {

    eraseBlock(l1 , r1);
    eraseBlock(l2 , r2);
    insertBlock(l1 , r2);

} 

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

    int n;
    cin >> n;

    set<pair<ll , ll>>free;
    for(int i = 1 ; i <= n ; ++i) {
        cin >> s[i];
        pref[i] = s[i];
        if(i - 2 >= 1) pref[i] += pref[i - 2];
        free.insert({s[i] , i});
    }


    int ans = 0;
    for(int i = 1 ; i <= (n + 1) / 2 ; ++i) {
        int value1 = -1e18 , value2 = -1e18;
        if(free.size()) value1 = prev(free.end()) -> F;
        if(blocks.size()) value2 = prev(blocks.end()) -> F;
        
        int L , R;
        if(value1 > value2) {
            ans += value1;
            int index = prev(free.end()) -> S;
            free.erase({s[index] , index});
            insertBlock(index , index);
            L = R = index;
        }
        else {
            L = prev(blocks.end()) -> S;
            R = r[L];
            eraseBlock(L , R);
            if(L == 1 || R == n) {
                --i;
                continue;
            }
            --L , ++R;
            ans += value2;
            insertBlock(L , R);
        }

        if(L - 1 >= 1) free.erase({s[L - 1] , L - 1});
        if(R + 1 <= n) free.erase({s[R + 1] , R + 1});
        
        if(L - 2 >= 1 && one[L - 2]) {
            merge(l[L - 2] , L - 2 , L , R);
            L = l[R];
        }
        if(R + 2 <= n && one[R + 2]) {
            merge(L , R , R + 2 , r[R + 2]);
            R = r[L];
        }

        cout << ans << endl;
    }

    return 0;
}

Compilation message

candies.cpp:39:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   39 | main() {
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10840 KB Output is correct
2 Correct 3 ms 10840 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 3 ms 10864 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 3 ms 10840 KB Output is correct
9 Correct 2 ms 10840 KB Output is correct
10 Correct 3 ms 10844 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 3 ms 10864 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10700 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 3 ms 10704 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 2 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 10840 KB Output is correct
2 Correct 3 ms 10840 KB Output is correct
3 Correct 2 ms 10844 KB Output is correct
4 Correct 3 ms 10864 KB Output is correct
5 Correct 3 ms 10844 KB Output is correct
6 Correct 2 ms 10844 KB Output is correct
7 Correct 3 ms 10844 KB Output is correct
8 Correct 3 ms 10840 KB Output is correct
9 Correct 2 ms 10840 KB Output is correct
10 Correct 3 ms 10844 KB Output is correct
11 Correct 3 ms 10844 KB Output is correct
12 Correct 3 ms 10864 KB Output is correct
13 Correct 3 ms 10844 KB Output is correct
14 Correct 3 ms 10700 KB Output is correct
15 Correct 3 ms 10844 KB Output is correct
16 Correct 3 ms 10844 KB Output is correct
17 Correct 2 ms 10844 KB Output is correct
18 Correct 3 ms 10704 KB Output is correct
19 Correct 2 ms 10844 KB Output is correct
20 Correct 2 ms 10844 KB Output is correct
21 Correct 254 ms 34996 KB Output is correct
22 Correct 279 ms 34776 KB Output is correct
23 Correct 251 ms 34900 KB Output is correct
24 Correct 120 ms 34644 KB Output is correct
25 Correct 118 ms 34756 KB Output is correct
26 Correct 113 ms 34644 KB Output is correct
27 Correct 142 ms 35124 KB Output is correct
28 Correct 156 ms 34840 KB Output is correct
29 Correct 145 ms 34896 KB Output is correct
30 Correct 157 ms 34844 KB Output is correct
31 Correct 163 ms 34884 KB Output is correct
32 Correct 140 ms 34904 KB Output is correct
33 Correct 220 ms 35016 KB Output is correct
34 Correct 197 ms 34784 KB Output is correct
35 Correct 206 ms 34736 KB Output is correct
36 Correct 265 ms 34868 KB Output is correct
37 Correct 260 ms 34896 KB Output is correct
38 Correct 261 ms 34816 KB Output is correct
39 Correct 128 ms 34628 KB Output is correct
40 Correct 125 ms 34796 KB Output is correct
41 Correct 121 ms 34640 KB Output is correct
42 Correct 139 ms 34864 KB Output is correct
43 Correct 158 ms 34896 KB Output is correct
44 Correct 147 ms 35396 KB Output is correct
45 Correct 142 ms 34900 KB Output is correct
46 Correct 139 ms 34856 KB Output is correct
47 Correct 152 ms 34880 KB Output is correct
48 Correct 205 ms 34644 KB Output is correct
49 Correct 198 ms 34796 KB Output is correct
50 Correct 232 ms 34632 KB Output is correct