Submission #7980

# Submission time Handle Problem Language Result Execution time Memory
7980 2014-08-26T14:56:55 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) < 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+1; 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 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 108 == 108
2 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 999 == 999
3 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 0 == 0
4 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 1542524 == 1542524
5 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 1 == 1
7 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 1 == 1
8 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 1 == 1
9 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 100400096 == 100400096
10 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 900320000 == 900320000
11 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 0 ms 85588 KB Output is correct - contestant found the optimal answer: 3200320000 == 3200320000
13 Incorrect 0 ms 85588 KB Output isn't correct - contestant didn't find the optimal answer: 140067 < 140072
14 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 -