답안 #967511

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
967511 2024-04-22T09:46:01 Z Tuanlinh123 Candies (JOI18_candies) C++17
100 / 100
261 ms 21224 KB
#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;

const ll maxn=200005;
ll a[maxn], pre[2][maxn];
set <pll> range, profit;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    ll n; cin >> n;
    for (ll i=1; i<=n; i++)
    {
        cin >> a[i];
        pre[i&1][i]=pre[i&1][i-1]+a[i];
        pre[i&1^1][i]=pre[i&1^1][i-1]-a[i];
    } 
    pre[0][n+1]=pre[0][n], pre[1][n+1]=pre[1][n];
    // calculate range sum, index l and r are chosen
    auto sum=[&](ll l, ll r)
    {
        assert((r-l)%2==0); l--, r++;
        return pre[r&1][r]-(l?pre[r&1][l-1]:0);
    };
    for (ll i=1; i<=n; i++) profit.insert({a[i], i});
    ll ans=0;
    for (ll i=1; i<=(n+1)/2; i++)
    {
        auto [x, p]=*prev(profit.end()); profit.erase(prev(profit.end())), ans+=x, cout << ans << "\n";
        auto ptr=range.lower_bound(mp(p, 0));
        ll l=p, r=p;
        if (ptr!=range.end() && (*ptr).fi==p+1)
            r=(*ptr).se+1, ptr=range.erase(ptr);
        if (ptr!=range.end() && (*ptr).fi==r+2)
            r=(*ptr).se, r<n?profit.erase({sum((*ptr).fi, (*ptr).se), (*ptr).fi-1}),0:0, ptr=range.erase(ptr);
        else if (r+1<=n) profit.erase({a[r+1], r+1});
        if (ptr!=range.begin() && (*prev(ptr)).se==l-2)
            l=(*prev(ptr)).fi, l>1?profit.erase({sum((*prev(ptr)).fi, (*prev(ptr)).se), l-1}),0:0, range.erase(prev(ptr));
        else if (l-1>=1) profit.erase({a[l-1], l-1});
        if (l>1 && r<n) profit.insert({sum(l, r), l-1});
        range.insert({l, r});
    }
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:25:14: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   25 |         pre[i&1^1][i]=pre[i&1^1][i-1]-a[i];
      |             ~^~
candies.cpp:25:28: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
   25 |         pre[i&1^1][i]=pre[i&1^1][i-1]-a[i];
      |                           ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2652 KB Output is correct
2 Correct 2 ms 2676 KB Output is correct
3 Correct 2 ms 2652 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 2 ms 2652 KB Output is correct
6 Correct 2 ms 2652 KB Output is correct
7 Correct 2 ms 2648 KB Output is correct
8 Correct 2 ms 2652 KB Output is correct
9 Correct 1 ms 2652 KB Output is correct
10 Correct 1 ms 2652 KB Output is correct
11 Correct 2 ms 2652 KB Output is correct
12 Correct 2 ms 2652 KB Output is correct
13 Correct 2 ms 2652 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 2 ms 2652 KB Output is correct
16 Correct 2 ms 2652 KB Output is correct
17 Correct 2 ms 2652 KB Output is correct
18 Correct 2 ms 2652 KB Output is correct
19 Correct 1 ms 2652 KB Output is correct
20 Correct 1 ms 2652 KB Output is correct
21 Correct 261 ms 20948 KB Output is correct
22 Correct 250 ms 20876 KB Output is correct
23 Correct 247 ms 20856 KB Output is correct
24 Correct 104 ms 20808 KB Output is correct
25 Correct 106 ms 21032 KB Output is correct
26 Correct 108 ms 20856 KB Output is correct
27 Correct 124 ms 21084 KB Output is correct
28 Correct 131 ms 21076 KB Output is correct
29 Correct 125 ms 21072 KB Output is correct
30 Correct 131 ms 21072 KB Output is correct
31 Correct 126 ms 21072 KB Output is correct
32 Correct 150 ms 21224 KB Output is correct
33 Correct 180 ms 20764 KB Output is correct
34 Correct 172 ms 20756 KB Output is correct
35 Correct 211 ms 21052 KB Output is correct
36 Correct 239 ms 20916 KB Output is correct
37 Correct 250 ms 20932 KB Output is correct
38 Correct 245 ms 21076 KB Output is correct
39 Correct 107 ms 20816 KB Output is correct
40 Correct 105 ms 20896 KB Output is correct
41 Correct 106 ms 20832 KB Output is correct
42 Correct 135 ms 21084 KB Output is correct
43 Correct 122 ms 20912 KB Output is correct
44 Correct 128 ms 21184 KB Output is correct
45 Correct 136 ms 21076 KB Output is correct
46 Correct 129 ms 21084 KB Output is correct
47 Correct 129 ms 21028 KB Output is correct
48 Correct 169 ms 20820 KB Output is correct
49 Correct 190 ms 20688 KB Output is correct
50 Correct 186 ms 21116 KB Output is correct