# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
397119 | tjdgus4384 | Split the sequence (APIO14_sequence) | C++14 | 763 ms | 88032 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>
using namespace std;
vector<long long> v, sum;
vector<pair<pair<long long, long long>, long long> > cht;
long long dp[100010], ans, n, k, p;
int pa[100010][210];
bool chk(long long p1, long long p2, pair<long long, long long> c){
pair<long long, long long> a = cht[p1].first;
pair<long long, long long> b = cht[p2].first;
if(b.first == c.first && b.second == c.second) return false;
if(a.first == b.first && a.second == b.second && p1 > 0) return chk(p1-1, p1, c);
if(a.first == b.first && a.second == b.second && c.second > b.second) return true;
if(a.first == b.first && a.second == b.second) return false;
if((a.second-c.second)*(b.first-a.first) <= (a.second-b.second)*(c.first-a.first)) return true;
else return false;
}
bool chk2(pair<long long, long long> a, pair<long long, long long> b, long long c){
if(b.first*c + b.second >= a.first*c + a.second) return true;
else return false;
}
int main(){
scanf("%lld %lld", &n, &k);
sum.resize(n); v.resize(n);
for(long long i = 0;i < n;i++) scanf("%lld", &v[i]);
for(long long i = 0;i < n;i++) for(long long j = 0;j < k;j++) pa[i][j] = -1;
sum[0] = v[0];
for(long long i = 1;i < n;i++) sum[i] = sum[i-1] + v[i];
for(long long i = 1;i < n;i++){
dp[i] = sum[i-1]*(sum[n-1]-sum[i-1]);
}
for(long long j = 1;j < k;j++){
for(long long i = 1;i < n;i++){
while(cht.size() >= 2 && chk(cht.size()-2, cht.size()-1, {sum[i-1], dp[i]-sum[i-1]*sum[n-1]})) cht.pop_back();
cht.push_back({{sum[i-1], dp[i]-sum[i-1]*sum[n-1]}, i});
}
long long x = 0;
for(long long i = j + 1;i < n;i++){
while(x < cht.size()-1 && cht[x+1].second < i && chk2(cht[x].first, cht[x+1].first, sum[i-1])) x++;
dp[i] = cht[x].first.first*sum[i-1] + cht[x].first.second + sum[i-1]*(sum[n-1]-sum[i-1]);
pa[i][j] = cht[x].second;
}
cht.clear();
}
for(long long i = k;i < n;i++){
if(ans <= dp[i]){
ans = dp[i];
p = i;
}
}
printf("%lld\n", ans);
vector<long long> ret;
while(p != -1){
ret.push_back(p);
p = pa[p][k-- -1];
}
reverse(ret.begin(), ret.end());
for(long long i = 0;i < ret.size();i++) printf("%lld ", ret[i]);
}
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... |