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 mod 1000000007
#define oo 1000000000
const double PI = acos(-1);
const int N = 100010 , K = 201;
int arr[N] , n , k , a, low , high , res , mid , best[N][K];
long long dp[N][K] , sum[N] , cur , cur2 ;
set < pair<int,int> > st;
set < pair<int,int> > :: iterator it ;
int main(){
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
sum[i+1] = sum[i] + arr[i];
}
for(int i=0;i<n;i++)
for(int j=0;j<k;j++)
dp[i][j] = -1e17;
for(int j=k-1;j>=0;j--){
st.clear();
st.insert(make_pair(n-1,n-1));
for(int i=n-2;i>=0;i--){
it = st.lower_bound(make_pair(i,0));
a = it->second;
dp[i][j] = dp[a][j+1] + (sum[a] - sum[i]) * (sum[n] - sum[a]);
best[i][j] = a;
low = 0 , high = i - 1 , res = -1;
while(high >= low){
mid = (low + high) / 2;
it = st.lower_bound(make_pair(mid,0));
assert(it != st.end());
a = it->second;
cur = dp[a][j+1] + (sum[a] - sum[mid]) * (sum[n] - sum[a]);
cur2 = dp[i][j+1] + (sum[i] - sum[mid]) * (sum[n] - sum[i]);
if(cur > cur2){
high = mid - 1;
}
else{
res = mid;
low = mid + 1;
}
}
it = st.lower_bound(make_pair(0,0));
while(it != st.end() && it->first <= res){
st.erase(it);
it = st.lower_bound(make_pair(0,0));
}
if(res != -1) st.insert(make_pair(res,i));
}
}
cout << dp[0][0] << endl;
int idx = 0;
for(int i=0;i<k;i++){
if(i > 0) putchar(' ');
printf("%d",best[idx][i]);
idx = best[idx][i];
}
puts("");
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:13:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&k);
^
sequence.cpp:15:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&arr[i]);
^
# | 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... |