Submission #31271

# Submission time Handle Problem Language Result Execution time Memory
31271 2017-08-17T16:09:32 Z nickyrio Split the sequence (APIO14_sequence) C++14
0 / 100
0 ms 1844 KB
#include <bits/stdc++.h>

using namespace std;
#define K 210
#define N 100010

    long long a[K][N], b[K][N], pos[K][N];
    double x[K][N];

struct Hull {

    int start, last;

    Hull(int n): start(1), last(0) {}

    void add(int k, long long aNew, long long bNew, long long pNew) {
        double xNew = -1e18;
        while(start <= last) {
            if (aNew == a[k][last]) {
                if (bNew >= b[k][last]) {
                    b[k][last] = bNew;
                    pos[k][last] = pNew;
                }
                return;
            }
            xNew = 1.0 * (b[k][last] - bNew) / (aNew - a[k][last]);
            if (start == last || xNew >= x[k][last]) break;
            last--;
        }
        a[k][++last] = aNew;
        b[k][last] = bNew;
        x[k][last] = xNew;
        pos[k][last] = pNew;
    }

    long long get(int k, long long xQuery) {
        if (start > last) return 0;
        while (start < last && x[k][start + 1] <= xQuery) start++;
        return start;
    }

};
vector<Hull> CH;

int n, k;
long long A[N], s[N], dp[K][N], pre[K][N];

void trace(int k, int u) {
    if (k != 1) trace(k - 1, pre[k][u]);
    printf("%d ", u);
}

int main() {
    //freopen("SEQUENCE.inp","r",stdin);
    //freopen("SEQUENCE.out","w",stdout);
    //ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    //cin>>n>>k;
    scanf("%d%d",&n, &k);
    Hull newHull(n);
    for (int i = 1; i<=n; i++) cin>>A[i];
    for (int i = 0; i<n; i++) CH.push_back(newHull);
    for (int i = 1; i<=n; i++) s[i] = A[i] + s[i - 1];
    memset(dp, 0, sizeof dp);
    for (int i = 1; i<=n; i++) {
        for (int kk = 0; kk<=min(i - 1, k - 1); kk++) {
            int tmp = CH[kk].get(kk, s[i]);
            pre[kk + 1][i] = pos[kk][tmp];
            dp[kk + 1][i] = b[kk][tmp] + s[i] * (a[kk][tmp] + s[n] - s[i]);
        }
        for (int kk = 0; kk<=min(i - 1, k - 1); kk++) {
            CH[kk + 1].add(kk + 1, s[i], dp[kk + 1][i] - s[n] * s[i], i);
        }
    }
    long long ans = k;
    for (int i = k + 1; i<n; i++) if (dp[k][ans] <= dp[k][i]) ans = i;
    printf("%lld\n",dp[k][ans]);//<<endl;
    trace(k, ans);
}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:59:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n, &k);
                         ^
# Verdict Execution time Memory Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 0 ms 1844 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -