Submission #158353

#TimeUsernameProblemLanguageResultExecution timeMemory
158353arnold518Split the sequence (APIO14_sequence)C++14
0 / 100
105 ms87508 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 1e5;
const int MAXK = 200;

int N, K;
ll A[MAXN+10], dp[MAXN+10], dp2[MAXN+10];
int memo[MAXN+10][MAXK+10];

struct CHT
{
    void init()
    {
        S.clear();
        pos=0;
    }

    struct Line { ll a, b, c; };
    double cross(const Line &p, const Line &q) { return (double)(q.b-p.b)/(p.a-q.a); }
    vector<Line> S;

    void update(Line p)
    {
        while(S.size()>1 && cross(S[S.size()-1], p)<=cross(S[S.size()-2], S[S.size()-1])) S.pop_back();
        S.push_back(p);
    }

    int pos=0;
    pll query(ll x)
    {
        if(S.size()<=pos) pos=S.size()-1;
        else while(pos+1<S.size() && cross(S[pos], S[pos+1])<=x) pos++;
        return {S[pos].a*x+S[pos].b, S[pos].c};
    }
};
CHT cht;

int main()
{
    int i, j;

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

    for(j=1; j<=K; j++)
    {
        cht.init();
        cht.update({0, -987654321, -1});
        for(i=1; i<=N; i++)
        {
            pll t=cht.query(A[i]);
            dp[i]=t.first;
            memo[i][j]=t.second;
            cht.update({A[i], dp2[i]-A[i]*A[i], i});
        }
        for(i=1; i<=N; i++) dp2[i]=dp[i];
        //for(i=1; i<=N; i++) printf("%lld ", dp[i]); printf("\n");
    }
    printf("%lld\n", dp[N]);
    int now=N;
    for(i=K; i>=1; i--)
    {
        now=memo[now][i];
        printf("%d ", now);
    }
}

Compilation message (stderr)

sequence.cpp: In member function 'pll CHT::query(ll)':
sequence.cpp:36:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if(S.size()<=pos) pos=S.size()-1;
            ~~~~~~~~^~~~~
sequence.cpp:37:25: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         else while(pos+1<S.size() && cross(S[pos], S[pos+1])<=x) pos++;
                    ~~~~~^~~~~~~~~
sequence.cpp: In function 'int main()':
sequence.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &N, &K);
     ~~~~~^~~~~~~~~~~~~~~~
sequence.cpp:48:45: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(i=1; i<=N; i++) scanf("%lld", &A[i]), A[i]+=A[i-1];
                         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
#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...