This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |