# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
750580 | M_W | Split the sequence (APIO14_sequence) | C++17 | 845 ms | 82004 KiB |
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>
#define ar array<long long, 3>
using namespace std;
long long dp[2][100001];
int pos[201][100001], qs[100001];
deque<ar> cht;
int idx = 0;
bool should_pop(ar cur, ar line1, ar line2){
if(cur[0] == line1[0] && cur[1] > line1[1]) return true;
return (line2[1] - cur[1]) * (cur[0] - line1[0]) >= (line1[1] - cur[1]) * (cur[0] - line2[0]);
}
long long f(ar line, long long x){
return line[0] * x + line[1];
}
pair<long long, int> query(long long x){
while(idx < cht.size() - 1 && f(cht[idx + 1], x) >= f(cht[idx], x)) cht.pop_front();
return {f(cht[0], x), cht[0][2]};
}
int main(int argc, char* argv[]){
int N, K; scanf("%d %d", &N, &K);
for(int i = 1; i <= N; i++){
scanf("%d", &qs[i]);
qs[i] += qs[i - 1];
}
long long ans = 0;
int maxj = 0;
for(int i = 1; i <= K; i++){
cht.push_back({qs[i - 1] * 1ll, dp[(i - 1) % 2][i - 1] - (qs[N] * 1ll * (qs[i - 1] * 1ll)), i - 1});
for(int j = i; j < N; j++){
long long const_val = qs[j] * 1ll * (qs[N] * 1ll - qs[j] * 1ll);
pair<long long, int> res = query(qs[j] * 1ll);
dp[i % 2][j] = const_val + res.first;
pos[i][j] = res.second;
if(dp[i % 2][j] >= ans){
ans = dp[i % 2][j];
maxj = j;
}
ar cur = {qs[j] * 1ll, dp[(i - 1) % 2][j] - (qs[N] * 1ll * (qs[j] * 1ll)), j};
while(cht.size() > 1){
int sz = cht.size();
if(should_pop(cur, cht[sz - 1], cht[sz - 2])) cht.pop_back();
else break;
}
if(cht.empty() || cur[0] > cht.back()[0] || (cur[0] == cht.back()[0] && cur[1] >= cht.back()[1])) cht.push_back(cur);
}
cht.clear();
}
printf("%lld\n", ans);
for(int i = K; i > 0; i--){
printf("%d ", maxj);
maxj = pos[i][maxj];
}
return 0;
}
Compilation message (stderr)
# | 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... |