Submission #800462

# Submission time Handle Problem Language Result Execution time Memory
800462 2023-08-01T15:08:33 Z vjudge1 Candies (JOI18_candies) C++17
100 / 100
476 ms 33652 KB

#include <bits/stdc++.h>


using namespace std;

#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"

const int N = 2e5 + 9 , mod = 1e9 + 7;
ll  d[N] = {} , a[N] = {}, dp[N][2] = {}, b[N] ,c[N] ;

multiset<pair<ll,pair<ll,ll>>>st , st1;


void del(ll x , ll y , ll z){
    st1.erase({x ,{y , z}});
    st.erase({z ,{x , y}});
}

void add(ll x , ll y , ll z){
    st1.insert({x ,{y , z}});
    st.insert({z ,{x , y}});
}


void solve(){
    ll q , i , j , m , n,  z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
    cin>>n;
    dp[0][0] = dp[0][1] = 0;
    for(i = 1; i <= n; i++){
        cin>>a[i];
        dp[i][0] = dp[i - 1][0];
        dp[i][1] = dp[i - 1][1];
        dp[i][i % 2] += a[i];
        add(i , i , a[i]);
    }
    l = 1 , r = n , s = 0;
    for(i = 1; i <= (n + 1) / 2; i++){
        auto it = st.rbegin();
        s += it->fi;
        z = it->fi , x = it->se.fi , y = it->se.se;
        del(x , y , z);
        cout<<s<<"\n";
        if(x == l || y == r){
            if(x == l){
                auto it1 = st1.lower_bound({y, {-1 , -1}});
                if(st1.size() && it1 != st1.end())
                    y = it1->se.fi , del(it1->fi , it1->se.fi,it1->se.se);
                l = y + 1;
            }
            if(y == r){
                auto it1 = st1.lower_bound({y , {-1 , -1}});
                if(st1.size() && it1 != st1.begin()){
                    it1--;
                    x = it1->fi;
                    del(it1->fi , it1->se.fi , it1->se.se);
                }
                r = x - 1;
            }
            continue;
        }
        auto it1 = st1.lower_bound({y , {-1 , -1}});
        if(st1.size() && it1 != st1.end()){
            y = it1->se.fi;
            del(it1->fi , it1->se.fi , it1->se.se);
        }
        it1 = st1.lower_bound({y , {-1 , -1}});
        if(st1.size() && it1 != st1.begin()){
            it1--;
            x = it1->fi;
            del(it1->fi , it1->se.fi , it1->se.se);
        }
        add(x , y , dp[y][x%2] - dp[x - 1][x%2] - (dp[y][1 - (x%2)] - dp[x - 1][1 - (x % 2)] ));
    }
}

int main(){

     TL;
     /*
     #ifndef ONLINE_JUDGE
     freopen("input.txt", "r", stdin);
     freopen("output.txt", "w", stdout);
     #endif
     */
int t = 1;
//cin>>t;

while(t--)
     {
     solve();
     }

}
// Author : حسن

Compilation message

candies.cpp: In function 'void solve()':
candies.cpp:41:8: warning: unused variable 'q' [-Wunused-variable]
   41 |     ll q , i , j , m , n,  z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |        ^
candies.cpp:41:16: warning: unused variable 'j' [-Wunused-variable]
   41 |     ll q , i , j , m , n,  z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                ^
candies.cpp:41:20: warning: unused variable 'm' [-Wunused-variable]
   41 |     ll q , i , j , m , n,  z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                    ^
candies.cpp:41:40: warning: unused variable 'f' [-Wunused-variable]
   41 |     ll q , i , j , m , n,  z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                        ^
candies.cpp:41:51: warning: unused variable 'k' [-Wunused-variable]
   41 |     ll q , i , j , m , n,  z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                                   ^
candies.cpp:41:67: warning: unused variable 'mn' [-Wunused-variable]
   41 |     ll q , i , j , m , n,  z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                                                   ^~
candies.cpp:41:80: warning: unused variable 'mx' [-Wunused-variable]
   41 |     ll q , i , j , m , n,  z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                                                                ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 2 ms 592 KB Output is correct
14 Correct 2 ms 660 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 7 ms 592 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 2 ms 596 KB Output is correct
20 Correct 2 ms 596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 596 KB Output is correct
2 Correct 2 ms 596 KB Output is correct
3 Correct 2 ms 596 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 2 ms 600 KB Output is correct
6 Correct 2 ms 596 KB Output is correct
7 Correct 2 ms 596 KB Output is correct
8 Correct 2 ms 596 KB Output is correct
9 Correct 2 ms 596 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 596 KB Output is correct
12 Correct 2 ms 596 KB Output is correct
13 Correct 2 ms 592 KB Output is correct
14 Correct 2 ms 660 KB Output is correct
15 Correct 2 ms 596 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 7 ms 592 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 2 ms 596 KB Output is correct
20 Correct 2 ms 596 KB Output is correct
21 Correct 465 ms 33468 KB Output is correct
22 Correct 464 ms 33564 KB Output is correct
23 Correct 466 ms 33444 KB Output is correct
24 Correct 207 ms 33232 KB Output is correct
25 Correct 210 ms 33248 KB Output is correct
26 Correct 224 ms 33348 KB Output is correct
27 Correct 218 ms 33488 KB Output is correct
28 Correct 224 ms 33496 KB Output is correct
29 Correct 225 ms 33532 KB Output is correct
30 Correct 236 ms 33388 KB Output is correct
31 Correct 232 ms 33464 KB Output is correct
32 Correct 233 ms 33492 KB Output is correct
33 Correct 315 ms 33164 KB Output is correct
34 Correct 334 ms 33248 KB Output is correct
35 Correct 318 ms 33380 KB Output is correct
36 Correct 476 ms 33480 KB Output is correct
37 Correct 471 ms 33400 KB Output is correct
38 Correct 465 ms 33424 KB Output is correct
39 Correct 221 ms 33476 KB Output is correct
40 Correct 212 ms 33252 KB Output is correct
41 Correct 214 ms 33304 KB Output is correct
42 Correct 222 ms 33436 KB Output is correct
43 Correct 217 ms 33484 KB Output is correct
44 Correct 225 ms 33652 KB Output is correct
45 Correct 235 ms 33424 KB Output is correct
46 Correct 232 ms 33432 KB Output is correct
47 Correct 231 ms 33476 KB Output is correct
48 Correct 330 ms 33264 KB Output is correct
49 Correct 341 ms 33248 KB Output is correct
50 Correct 332 ms 33260 KB Output is correct