Submission #337216

# Submission time Handle Problem Language Result Execution time Memory
337216 2020-12-18T21:27:06 Z bigDuck Split the sequence (APIO14_sequence) C++14
100 / 100
1361 ms 93184 KB
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll

int t, n, m, k, a[100010], q, l, r;
int p[100010];
int dp[100010][2];
int dp1[1010][210];

int32_t dir[100010][210];



bool upper(pair<pair<int, int>, pii> l1, pair<pair<int,int>, pii> l2){
    return ( (l1.sc.ft*(l2.ft.ft)+l1.sc.sc)>(l2.sc.ft*(l2.ft.ft)+l2.sc.sc)  );
}


int intersect(pair<pair<int, int>, pii> l1, pair<pair<int, int>, pii> l2){
    int a1=l1.sc.ft, b1=l1.sc.sc, a2=l2.sc.ft, b2=l2.sc.sc;
    if( ((b2-b1)%(a1-a2))==0 ){
        return ( ((b2-b1)/(a1-a2))  );
    }
    else{
        return ( ((b2-b1)/(a1-a2))+1  );
    }
}


void convex_hull(int g){

vector<pair<pair<int, int>, pii> > st;
for(int j=n; j>=((g==1)?(0):(1)); j--){
        pair<pair<int,int >, pii> ac={ {0ll, j}, {-p[j], dp[j][0]}};
    while( (!st.empty()) && ((upper(ac, st.back())) ) ){
        st.pop_back();
    }
    if(st.empty()){
        st.pb(ac);
    }
    else{
            if(ac.sc.ft==st.back().sc.ft){ac.ft.ft=st.back().ft.ft; st.pb(ac);}
            else{
        st.pb({{intersect(ac, st.back()), j}, ac.sc});}
    }
}

int j=0;
for(int i=n; i>=1; i--){
    while((j<(st.size()-1) ) && ( ( ((st[j+1].ft.ft<=(p[n]-p[i])) && (st[j].ft.ft<(p[n]-p[i])) ) || (st[j].ft.sc==i) )) ){
        j++;
    }

    dp[i][1]=(p[n]-p[i])*st[j].sc.ft+st[j].sc.sc+p[i]*(p[n]-p[i]);
    dir[i][g]=st[j].ft.sc;
}



for(int i=1; i<=n ;i++){
    dp[i][0]=dp[i][1];
}

}










int32_t main(){
INIT







cin>>n>>k;

if(n<=1000){
for(int i=1; i<=n ;i++){
    cin>>a[i];
    p[i]=p[i-1]+a[i];
}

dp1[0][0]=0;
for(int g=1; g<=(k+1); g++){
    dp1[0][g]=-1;
}
k++;
vector<int> v;
for(int i=1; i<=n; i++){
    for(int g=0; g<=k; g++){
        dp1[i][g]=-1;
    }
    for(int j=i-1; j>=0; j--){
        for(int g=0; g<k; g++){
            if(dp1[j][g]==-1){continue;}
            if(dp1[i][g+1]==(-1)){
                dp1[i][g+1]=dp1[j][g]+(p[i]-p[j])*(p[i]-p[j]);
                dir[i][g+1]=j;
            }
            else{
                    if(  (dp1[j][g]+(p[i]-p[j])*(p[i]-p[j]))<dp1[i][g+1] ){
                        dir[i][g+1]=j;
                    }
                dp1[i][g+1]=min(dp1[i][g+1], dp1[j][g]+(p[i]-p[j])*(p[i]-p[j]));
            }
        }
    }
}

cout<<( ((p[n]*p[n])-dp1[n][k])/2 );
cout<<"\n";
int cnt=1, cr=n;
while(cnt<k){
    v.pb(dir[cr][k-cnt+1]);
    cr=dir[cr][k-cnt+1];
    cnt++;
}
for(int i=k-1-1; i>=0; i--){
    cout<<v[i]<<" ";
}
return 0;
}






for(int i=1; i<=n; i++){
    cin>>a[i];
    p[i]=(p[i-1]+a[i]);
}


for(int g=1; g<=(k+1); g++){
    convex_hull(g);
}

cout<<(dp[n][0] )<<"\n"<<flush;
int cnt=0, cr=n;
while(cnt<k){
    cout<<dir[cr][k-cnt+1]<<" "; cr=dir[cr][k-cnt+1]; cnt++;
}


return 0;
}



Compilation message

sequence.cpp: In function 'void convex_hull(long long int)':
sequence.cpp:58:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     while((j<(st.size()-1) ) && ( ( ((st[j+1].ft.ft<=(p[n]-p[i])) && (st[j].ft.ft<(p[n]-p[i])) ) || (st[j].ft.sc==i) )) ){
      |            ~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 364 KB contestant found the optimal answer: 999 == 999
3 Correct 0 ms 364 KB contestant found the optimal answer: 0 == 0
4 Correct 0 ms 364 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 1 ms 364 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 364 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 384 KB contestant found the optimal answer: 1 == 1
8 Correct 1 ms 364 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 384 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 364 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 0 ms 364 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 384 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 384 KB contestant found the optimal answer: 140072 == 140072
14 Correct 0 ms 364 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 364 KB contestant found the optimal answer: 805 == 805
16 Correct 0 ms 364 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 0 ms 364 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 1 ms 492 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 1 ms 492 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 1 ms 492 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 1 ms 492 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 1 ms 512 KB contestant found the optimal answer: 933702 == 933702
7 Correct 1 ms 504 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 1 ms 492 KB contestant found the optimal answer: 687136 == 687136
9 Correct 1 ms 492 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 1 ms 492 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 1 ms 876 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 876 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 7 ms 896 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 1 ms 896 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 6 ms 876 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 9 ms 876 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 7 ms 896 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 3 ms 876 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 3 ms 876 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 3 ms 876 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2796 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 8 ms 2796 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 216 ms 2860 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 8 ms 2924 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 220 ms 2796 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 189 ms 2796 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 221 ms 2924 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 217 ms 2796 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 56 ms 2796 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 102 ms 2796 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 8 ms 9776 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 7 ms 9324 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 99 ms 9924 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 7 ms 9392 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 82 ms 9776 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 41 ms 9392 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 80 ms 9760 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 68 ms 9860 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 76 ms 9776 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 95 ms 9900 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 64 ms 92516 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 60 ms 91812 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 1320 ms 93180 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 65 ms 92160 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 1361 ms 93164 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 717 ms 92044 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 1010 ms 92936 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 798 ms 92916 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 940 ms 93044 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 1201 ms 93184 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845