답안 #1046561

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1046561 2024-08-06T16:59:09 Z underwaterkillerwhale Candies (JOI18_candies) C++17
100 / 100
68 ms 15308 KB
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(),v.end()

using namespace std;

mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }

const int N = 2e5 + 7;
const int Mod = 1e9 + 7;
const int INF = 1e9;
const ll BASE = 137;

int n;
ll a[N];
int nxt[N], prv[N];
bool del[N];
ll val[N];

void solution () {
    cin >> n;
    rep (i, 1, n) cin >> a[i];
    struct Data {
        ll val, l, r;
    };
    struct cmp {
        bool operator () (Data A, Data B) { return A.val < B.val; }
    };
    priority_queue<Data, vector<Data>, cmp> S;
    rep (i, 1, n) {
        S.push({a[i], i, i});
        nxt[i] = i;
        prv[i] = i;
    }
    nxt[0] = prv[0] = 0;
    nxt[n + 1] = prv[n + 1] = n + 1;
    ll res = 0;
    while (!S.empty()) {
        Data cur = S.top();
        S.pop();
        if (del[cur.l] || del[cur.r] || del[cur.l - 1] || del[cur.r + 1]) {
            continue;
        }
//        cout << cur.l <<" "<<cur.r <<"\n";
        del[cur.l] = del[cur.r] = 1;
        res += cur.val;
        if (del[cur.r + 2] || del[cur.l - 2]) {
            ll nval = cur.val, added = 0, nl = cur.l - 1, nr = cur.r + 1;
            if (del[cur.r + 2]) nval += val[cur.r + 2], added += a[cur.r + 1], nr = nxt[cur.r + 2];
            if (del[cur.l - 2]) nval += val[cur.l - 2], added += a[cur.l - 1], nl = prv[cur.l - 2];
            val[nl + 1] = val[nr - 1] = nval - added;
            nxt[nl + 1] = nxt[nr - 1] = nr;
            prv[nl + 1] = prv[nr - 1] = nl;
//            cout <<a[nl] + a[nr] + added - nval <<" "<<nl <<" "<<nr <<"\n";
            if (nl > 0 && nr < n + 1)
                S.push({a[nl] + a[nr] + added - nval, nl, nr});
        }
        else {
            val[cur.l] = val[cur.r] = cur.val;
            nxt[cur.l] = nxt[cur.r] = cur.r + 1;
            prv[cur.l] = prv[cur.r] = cur.l - 1;
            if (cur.l - 1 > 0 && cur.r + 1 < n + 1)
                S.push({a[cur.r + 1] + a[cur.l - 1] - cur.val, cur.l - 1, cur.r + 1});
        }
        cout << res  <<"\n";
    }
}


#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
//    file("c");
    ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int num_Test = 1;
//    cin >> num_Test;
    while (num_Test--)
        solution();
}
/*
no bug challenge +2
4
1 2 3 4
2
0 3
2 1
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 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 4700 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 4588 KB Output is correct
13 Correct 1 ms 4720 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 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 4700 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4696 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 4700 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 4588 KB Output is correct
13 Correct 1 ms 4720 KB Output is correct
14 Correct 1 ms 4700 KB Output is correct
15 Correct 1 ms 4700 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 4700 KB Output is correct
19 Correct 1 ms 4700 KB Output is correct
20 Correct 1 ms 4700 KB Output is correct
21 Correct 66 ms 15308 KB Output is correct
22 Correct 65 ms 14272 KB Output is correct
23 Correct 68 ms 14512 KB Output is correct
24 Correct 46 ms 14252 KB Output is correct
25 Correct 45 ms 15048 KB Output is correct
26 Correct 44 ms 15036 KB Output is correct
27 Correct 42 ms 14268 KB Output is correct
28 Correct 42 ms 14788 KB Output is correct
29 Correct 44 ms 15308 KB Output is correct
30 Correct 50 ms 14012 KB Output is correct
31 Correct 42 ms 14528 KB Output is correct
32 Correct 42 ms 14792 KB Output is correct
33 Correct 48 ms 14524 KB Output is correct
34 Correct 48 ms 14268 KB Output is correct
35 Correct 48 ms 14136 KB Output is correct
36 Correct 66 ms 14776 KB Output is correct
37 Correct 64 ms 14528 KB Output is correct
38 Correct 65 ms 14788 KB Output is correct
39 Correct 45 ms 14396 KB Output is correct
40 Correct 44 ms 15052 KB Output is correct
41 Correct 45 ms 15116 KB Output is correct
42 Correct 42 ms 15308 KB Output is correct
43 Correct 46 ms 14988 KB Output is correct
44 Correct 44 ms 14272 KB Output is correct
45 Correct 46 ms 15044 KB Output is correct
46 Correct 43 ms 14540 KB Output is correct
47 Correct 43 ms 14360 KB Output is correct
48 Correct 50 ms 14004 KB Output is correct
49 Correct 49 ms 14540 KB Output is correct
50 Correct 49 ms 15040 KB Output is correct