Submission #7081

#TimeUsernameProblemLanguageResultExecution timeMemory
7081baneling100Split the sequence (APIO14_sequence)C++98
0 / 100
0 ms131072 KiB
#include <stdio.h>

long long N, K, A[100001], D[2][100001], Path[201][100001], S[100001], Top, Ans[201];

void input(void)
{
    int i;

    scanf("%lld %lld",&N,&K);
    for(i=1 ; i<=N ; i++)
    {
        scanf("%lld",&A[i]);
        A[i]+=A[i-1];
    }
}

void process(void)
{
    long long i, j, k, x, a, b, c, d, e, f, now;

    for(j=1 ; j<=K ; j++)
    {
        x=j%2;
        Top=0;
        now=1;
        for(k=j ; k<=N ; k++)
        {
            if(Top<2)
                S[++Top]=k;
            else
            {
                a=A[S[Top-1]];
                b=D[1-x][S[Top-1]]-A[S[Top-1]]*A[S[Top-1]];
                c=A[S[Top]];
                d=D[1-x][S[Top]]-A[S[Top]]*A[S[Top]];
                e=A[k];
                f=D[1-x][k]-A[k]*A[k];
                while((d-b)*(e-a)<=(c-a)*(f-b) && Top>=2)
                {
                    Top--;
                    a=A[S[Top-1]];
                    b=D[1-x][S[Top-1]]-A[S[Top-1]]*A[S[Top-1]];
                    c=A[S[Top]];
                    d=D[1-x][S[Top]]-A[S[Top]]*A[S[Top]];
                }
                S[++Top]=k;
            }
            a=A[S[now]]*A[k+1]+D[1-x][S[now]]-A[S[now]]*A[S[now]];
            b=A[S[now+1]]*A[k+1]+D[1-x][S[now+1]]-A[S[now+1]]*A[S[now+1]];
            while(now<k && a<b)
            {
                now++;
                a=A[S[now]]*A[k+1]+D[1-x][S[now]]-A[S[now]]*A[S[now]];
                b=A[S[now+1]]*A[k+1]+D[1-x][S[now+1]]-A[S[now+1]]*A[S[now+1]];
            }
            D[x][k+1]=a;
            Path[j][k+1]=S[now];
        }
    }
    a=K;
    b=N;
    while(a)
    {
        Ans[a]=Path[a][b];
        b=Path[a][b];
        a--;
    }
}

void output(void)
{
    long long i;

    printf("%lld\n",D[K%2][N]);
    for(i=1 ; i<=K ; i++)
        printf("%lld ",Ans[i]);

}

int main(void)
{
    input();
    process();
    output();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...