Submission #58269

# Submission time Handle Problem Language Result Execution time Memory
58269 2018-07-17T10:32:02 Z alenam0161 Candies (JOI18_candies) C++17
100 / 100
737 ms 46480 KB
#include <bits/stdc++.h>
using namespace std;
const int N = 200000+7;
int a[N];
int l[N],r[N],p[N];
long long val[N];
int sz[N];
int fnd(int v){
    return v==p[v] ? v:p[v]=fnd(p[v]);
}
int main(){

    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;++i)scanf("%d",&a[i]);
    for(int i=1;i<=n;++i){
        r[i]=l[i]=p[i]=i;
        val[i]=a[i];
        sz[i]=1;
    }
    multiset<pair<long long,int> > st;
    for(int i=1;i<=n;++i){
        st.insert({a[i],i});
    }
    int hw=(n+1)/2;
    long long ans=0;
    p[n+1]=n+1;
    p[0]=0;
    while(st.size()>0){
        auto it=st.end();it--;
        long long vl=it->first;
        int ps=it->second;
        ps=fnd(ps);
        ans+=vl;
        hw--;
        cout<<ans<<endl;
        if(hw==0)return 0;
        st.erase(st.find({vl,ps}));
        val[ps]*=-1;
        int pl=l[ps]-1;pl=fnd(pl);
        int pr=r[ps]+1;pr=fnd(pr);
        if(l[ps]==1){
            p[ps]=pr;
            l[pr]=l[ps];
            st.erase(st.find({val[pr],pr}));
            if(r[pr]<n){
                int pxr=r[pr]+1;pxr=fnd(pxr);
                p[pr]=p[pxr];
                l[pxr]=l[pr];
            }
            else {
                val[pr]*=-1;
                val[pr]+=val[ps];
                st.insert({val[pr],pr});
            }
        }
        else if(r[ps]==n){
            p[ps]=pl;
            r[pl]=r[ps];
            st.erase(st.find({val[pl],pl}));
            if(l[pl]!=1){
                int pxl=l[pl]-1;pxl=fnd(pxl);
                p[pl]=p[pxl];
                r[pxl]=r[pl];
            }
            else {
                val[pl]*=-1;
                val[pl]+=val[ps];
                st.insert({val[pl],pl});
            }
        }
        else{
            if(l[ps]>1){
                pl=fnd(pl);
                val[ps]+=val[pl];
                l[ps]=l[pl];
                p[pl]=ps;
                st.erase(st.find({val[pl],pl}));
            }
            if(r[ps]<n){
                pr=fnd(pr);
                val[ps]+=val[pr];
                r[ps]=r[pr];
                p[pr]=ps;
                st.erase(st.find({val[pr],pr}));
            }
            st.insert({val[ps],ps});
        }
    }
    return 0;
}

Compilation message

candies.cpp: In function 'int main()':
candies.cpp:14:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&n);
     ~~~~~^~~~~~~~~
candies.cpp:15:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1;i<=n;++i)scanf("%d",&a[i]);
                          ~~~~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 744 KB Output is correct
3 Correct 5 ms 744 KB Output is correct
4 Correct 5 ms 744 KB Output is correct
5 Correct 5 ms 744 KB Output is correct
6 Correct 5 ms 864 KB Output is correct
7 Correct 5 ms 864 KB Output is correct
8 Correct 5 ms 864 KB Output is correct
9 Correct 4 ms 864 KB Output is correct
10 Correct 4 ms 892 KB Output is correct
11 Correct 5 ms 912 KB Output is correct
12 Correct 6 ms 916 KB Output is correct
13 Correct 5 ms 1084 KB Output is correct
14 Correct 5 ms 1096 KB Output is correct
15 Correct 6 ms 1096 KB Output is correct
16 Correct 5 ms 1148 KB Output is correct
17 Correct 5 ms 1148 KB Output is correct
18 Correct 5 ms 1148 KB Output is correct
19 Correct 4 ms 1148 KB Output is correct
20 Correct 5 ms 1184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 504 KB Output is correct
2 Correct 5 ms 744 KB Output is correct
3 Correct 5 ms 744 KB Output is correct
4 Correct 5 ms 744 KB Output is correct
5 Correct 5 ms 744 KB Output is correct
6 Correct 5 ms 864 KB Output is correct
7 Correct 5 ms 864 KB Output is correct
8 Correct 5 ms 864 KB Output is correct
9 Correct 4 ms 864 KB Output is correct
10 Correct 4 ms 892 KB Output is correct
11 Correct 5 ms 912 KB Output is correct
12 Correct 6 ms 916 KB Output is correct
13 Correct 5 ms 1084 KB Output is correct
14 Correct 5 ms 1096 KB Output is correct
15 Correct 6 ms 1096 KB Output is correct
16 Correct 5 ms 1148 KB Output is correct
17 Correct 5 ms 1148 KB Output is correct
18 Correct 5 ms 1148 KB Output is correct
19 Correct 4 ms 1148 KB Output is correct
20 Correct 5 ms 1184 KB Output is correct
21 Correct 629 ms 22404 KB Output is correct
22 Correct 653 ms 24412 KB Output is correct
23 Correct 648 ms 26276 KB Output is correct
24 Correct 341 ms 27952 KB Output is correct
25 Correct 356 ms 29804 KB Output is correct
26 Correct 345 ms 31660 KB Output is correct
27 Correct 344 ms 33532 KB Output is correct
28 Correct 344 ms 35744 KB Output is correct
29 Correct 348 ms 37496 KB Output is correct
30 Correct 356 ms 39400 KB Output is correct
31 Correct 353 ms 41360 KB Output is correct
32 Correct 359 ms 43312 KB Output is correct
33 Correct 494 ms 45140 KB Output is correct
34 Correct 506 ms 46220 KB Output is correct
35 Correct 524 ms 46296 KB Output is correct
36 Correct 737 ms 46380 KB Output is correct
37 Correct 666 ms 46480 KB Output is correct
38 Correct 650 ms 46480 KB Output is correct
39 Correct 339 ms 46480 KB Output is correct
40 Correct 341 ms 46480 KB Output is correct
41 Correct 337 ms 46480 KB Output is correct
42 Correct 342 ms 46480 KB Output is correct
43 Correct 349 ms 46480 KB Output is correct
44 Correct 378 ms 46480 KB Output is correct
45 Correct 361 ms 46480 KB Output is correct
46 Correct 369 ms 46480 KB Output is correct
47 Correct 354 ms 46480 KB Output is correct
48 Correct 495 ms 46480 KB Output is correct
49 Correct 492 ms 46480 KB Output is correct
50 Correct 485 ms 46480 KB Output is correct