Submission #7979

# Submission time Handle Problem Language Result Execution time Memory
7979 2014-08-26T14:55:35 Z gs14004 Split the sequence (APIO14_sequence) C++
0 / 100
0 ms 85588 KB
#include <cstdio>
#include <utility>

#include <vector>

using namespace std;
typedef long long lint;
typedef pair<lint, int> pi;

struct cht{
    int s,e;
    lint la[100005],lb[100005];
    int label[100005];
    double cross(int a, int b){return (double)(lb[b]-lb[a])/(la[a]-la[b]);}
    void insert(lint p, lint q, int piv){
        la[e] = p;
        lb[e] = q;
        label[e] = piv;
        while(e>1 && (lb[e-2] - lb[e-1]) * (la[e] - la[e-1]) > (lb[e-1] - lb[e]) *(la[e-1] - la[e-2])){
            la[e-1] = la[e];
            lb[e-1] = lb[e];
            label[e-1] = label[e];
            e--;
        }
        e++;
    }
    pi query(lint x){
        while(s+1<e && cross(s,s+1) < (double) x) s++;
        return pi(la[s]*x+lb[s],label[s]);
    }
    void clear(){s = e = 0;}
}convexHull;

int n,k;
lint dp[100005][2],a[100005];
int track[100005][205];

int main(){
    int t;
    scanf("%d %d",&n,&k);
    for (int i=0; i<n; i++) {
        scanf("%d",&t);
        a[i+1] = a[i] + t;
    }
    for (int i=1; i<=n; i++) {
        dp[i][0] = (a[n] - a[i]) * a[i];
    }
    if(k == 1){
        int piv = 1;
        for (int i=1; i<=n; i++) {
            if(dp[i][0] > dp[piv][0]){
                piv = i;
            }
        }
   //     printf("%lld\n%d",dp[piv][0], piv);
        return 0;
    }
    for (int t=1; t<=k; t++) {
        convexHull.clear();
        dp[t][t%2] = -1e9;
      //  convexHull.insert(a[t], dp[t][(t-1)%2] - a[n]*a[t], t);
        printf("%lld ",dp[t][t%2]);
        for (int i=t; i<=n; i++) {
            pi x = convexHull.query(a[i]);
            track[i][t] = x.second;
            dp[i][t%2] = x.first + a[i] * (a[n] - a[i]);
            convexHull.insert(a[i],dp[i][(t-1)%2]-a[n]*a[i], i);
            printf("%lld ",dp[i][t%2]);
        }
        pi x = convexHull.query(a[n]);
        track[n+1][t] = x.second;
        dp[n+1][t%2] = x.first;
        puts("");
    }
    printf("%lld\n",dp[n][k%2]);
    vector<int> res;
    int pos1 = n;
    for(int pos2 = k; pos2; pos2--){
        res.push_back(track[pos1][pos2]);
        pos1 = track[pos1][pos2];
    }
    while (!res.empty()) {
        printf("%d ",res.back());
        res.pop_back();
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 85588 KB Output isn't correct - Integer 52 violates the range [1, 6]
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 -