Submission #6900

# Submission time Handle Problem Language Result Execution time Memory
6900 2014-07-09T16:39:58 Z dohyun0324 Split the sequence (APIO14_sequence) C++
0 / 100
0 ms 131072 KB
#include<stdio.h>
long long int n,k,a[100010],s[100010],d[210][100010],top,p,path[210][100010],w,dap[100010];
struct data
{
    long long int m,n,pos;
}st[100010];
int main()
{
    long long int i,j,r,m1,m2,n1,n2,m3,n3;
    scanf("%lld %lld",&n,&k);
    for(i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
        s[i]=s[i-1]+a[i];
    }
    for(i=2;i<=k+1;i++)
    {
        top=1, p=1;
        st[1].m=s[1]; st[1].n=-s[1]*s[1]+d[i-1][1]; st[1].pos=1;
        for(j=2;j<=n;j++)
        {
            m2=s[j]; n2=-s[j]*s[j]+d[i-1][j];
            for(r=top;r>=1;r--)
            {
                if(r==1) break;
                m1=st[r].m; n1=st[r].n; m3=st[r-1].m; n3=st[r-1].n;
                if((n1-n2)*(m1-m3)>(m2-m1)*(n3-n1)) break;
                top--;
            }
            top++; st[top].m=m2; st[top].n=n2; st[top].pos=j;
            for(r=p;r<=top-1;r++)
            {
                m1=st[r+1].m; n1=st[r+1].n; m3=st[r].m; n3=st[r].n;
                if(n3-n1>(m1-m3)*s[j]) break;
            }
            p=r;
            d[i][j]=st[p].m*s[j]+st[p].n; path[i][j]=st[p].pos;
        }
    }
    printf("%lld\n",d[k+1][n]);
    p=n;
    for(i=k+1;i>=1;i--)
    {
        if(p==0) break;
        dap[++w]=p;
        p=path[i][p];
    }
    for(i=w;i>=2;i--) printf("%lld ",dap[i]);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Memory limit exceeded 0 ms 131072 KB Memory limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Halted 0 ms 0 KB -