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
int a[N], b[N], pos[N];
double x[N];
int n, k;
int A[N], s[N], pre[K][N];
long long dp[N];
struct Hull {
int start, last;
Hull(int n): start(1), last(0) {}
void init() {
start = 1; last = 0;
}
void add(int k, long long aNew, long long bNew, int pNew) {
double xNew = -1e18;
while(start <= last) {
if (aNew == a[last]) {
if (bNew >= b[last]) {
b[last] = bNew;
pos[last] = pNew;
}
return;
}
xNew = 1.0 * (b[last] - bNew) / (aNew - a[last]);
if (start == last || xNew >= x[last]) break;
last--;
}
a[++last] = aNew;
b[last] = bNew;
x[last] = xNew;
pos[last] = pNew;
}
long long get(int k, int xQuery) {
if (start > last) return 0;
while (start < last && pos[start + 1] <= xQuery && x[start + 1] <= s[xQuery]) start++;
return start;
}
};
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);
for (int i = 1; i<=n; i++) cin>>A[i];
for (int i = 1; i<=n; i++) s[i] = A[i] + s[i - 1];
memset(dp, 0, sizeof dp);
Hull CH(n);
for (int kk = 0; kk<= k - 1; kk++) {
for (int i = 1; i<=n; i++) {
int tmp = CH.get(kk, i);
pre[kk + 1][i] = pos[tmp];
dp[i] = b[tmp] + s[i] * (a[tmp] + s[n] - s[i]);
}
CH.init();
for (int i = 1; i<=n; i++) {
CH.add(kk + 1, s[i], dp[i] - s[n] * s[i], i);
}
}
long long ans = k;
for (int i = k + 1; i<n; i++) if (dp[ans] <= dp[i]) ans = i;
printf("%lld\n",dp[ans]);//<<endl;
trace(k, ans);
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:60: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... |