Submission #551574

# Submission time Handle Problem Language Result Execution time Memory
551574 2022-04-21T05:00:18 Z ToroTN Split the sequence (APIO14_sequence) C++14
100 / 100
647 ms 87716 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define X first
#define Y second
struct line
{
    ll m,c;
};
ll n,m,dp[100005][2],a[100005],qs[100005],u,g;
ll sum[100005],pos,cut;
int back[100005][205];
line t,now,pre;
stack<ll> stk;
vector<pair<line,ll> > v;
ll cal(line node,ll ptr)
{
    return node.m*sum[ptr]+node.c;
}
ll intersect(line l1,line l2,line now)
{
    return (now.c-l1.c)*(l2.m-now.m)-(now.c-l2.c)*(l1.m-now.m)<=0;
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i],qs[i]=qs[i-1]+a[i]*a[i];
    for(int i=0;i<=n;i++)for(int j=0;j<=1;j++)dp[i][j]=2e18;
    dp[0][0]=0;
    memset(back,-1,sizeof back);
    for(int i=1;i<=m+1;i++)
    {
        for(int j=0;j<=n;j++)dp[j][i%2]=2e18;
        for(int j=0;j<=n;j++)
        {
            if(pos>=v.size())pos=v.size()-1;
            while(pos+1<v.size()&&cal(v[pos+1].X,j)<=cal(v[pos].X,j))pos++;
            if(v.size()>0)
            {
                dp[j][i%2]=cal(v[pos].X,j)+sum[j]*sum[j]-qs[j];
                back[j][i]=v[pos].Y;
            }
            if(j<i-1)continue;
            t={-2*sum[j],dp[j][1-(i%2)]+(sum[j]*sum[j]+qs[j])};
            while(v.size()>=1)
            {
                now=v[v.size()-1].X;
                if(now.m==t.m&&t.c<=now.c)
                {
                    v.pop_back();
                }else
                {
                    break;
                }
            }
            while(v.size()>=2)
            {
                now=v[v.size()-1].X;
                pre=v[v.size()-2].X;
                cut=intersect(now,pre,t);
                if(cut)
                {
                    v.pop_back();
                }else
                {
                    break;
                }
            }
            v.pb({t,j});
            //printf("%d %d %lld\n",i,j,dp[j][i%2]);
        }
        while(!v.empty())v.pop_back();
    }
    u=n;
    g=m+1;
    while(1)
    {
        u=back[u][g];
        --g;
        if(u==0)break;
        stk.push(u);
    }
    printf("%lld\n",(sum[n]*sum[n]-qs[n]-dp[n][(m+1)%2])/2);
    while(!stk.empty())
    {
        printf("%lld ",stk.top());
        stk.pop();
    }
    printf("\n");
}
/*
7 3
4 1 3 4 0 2 3
*/

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:37:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<line, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |             if(pos>=v.size())pos=v.size()-1;
      |                ~~~^~~~~~~~~~
sequence.cpp:38:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<line, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |             while(pos+1<v.size()&&cal(v[pos+1].X,j)<=cal(v[pos].X,j))pos++;
      |                   ~~~~~^~~~~~~~~
sequence.cpp:27:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |     scanf("%lld%lld",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~~~~~
sequence.cpp:28:31: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i],qs[i]=qs[i-1]+a[i]*a[i];
      |                          ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 36 ms 80528 KB contestant found the optimal answer: 108 == 108
2 Correct 32 ms 80460 KB contestant found the optimal answer: 999 == 999
3 Correct 31 ms 80616 KB contestant found the optimal answer: 0 == 0
4 Correct 32 ms 80560 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 32 ms 80524 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 32 ms 80456 KB contestant found the optimal answer: 1 == 1
7 Correct 33 ms 80480 KB contestant found the optimal answer: 1 == 1
8 Correct 31 ms 80528 KB contestant found the optimal answer: 1 == 1
9 Correct 33 ms 80420 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 30 ms 80468 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 32 ms 80556 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 32 ms 80524 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 33 ms 80432 KB contestant found the optimal answer: 140072 == 140072
14 Correct 31 ms 80556 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 32 ms 80476 KB contestant found the optimal answer: 805 == 805
16 Correct 34 ms 80460 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 32 ms 80468 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 32 ms 80516 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 31 ms 80572 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 30 ms 80520 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 34 ms 80528 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 33 ms 80544 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 33 ms 80456 KB contestant found the optimal answer: 933702 == 933702
7 Correct 31 ms 80444 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 32 ms 80460 KB contestant found the optimal answer: 687136 == 687136
9 Correct 31 ms 80468 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 30 ms 80480 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 32 ms 80468 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 39 ms 80520 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 34 ms 80500 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 39 ms 80460 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 31 ms 80460 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 31 ms 80560 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 33 ms 80464 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 32 ms 80468 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 33 ms 80472 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 34 ms 80536 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 34 ms 80536 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 36 ms 80552 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 38 ms 80592 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 30 ms 80596 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 35 ms 80560 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 36 ms 80508 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 36 ms 80596 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 34 ms 80596 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 31 ms 80496 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 32 ms 80600 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 33 ms 81208 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 34 ms 81228 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 68 ms 81372 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 34 ms 81416 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 60 ms 81236 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 61 ms 81236 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 63 ms 81404 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 62 ms 81356 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 64 ms 81448 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 64 ms 81348 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 53 ms 87600 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 53 ms 87624 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 551 ms 87716 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 54 ms 87648 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 647 ms 87648 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 462 ms 87592 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 444 ms 87628 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 379 ms 87644 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 416 ms 87620 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 518 ms 87608 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845