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 fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
const int N=1e5+10;
deque<int>deq;
ll dp[N],dp2[N];
int sp[N],t[N];
int bb[N][201];
ll wz(int j,int i){
return dp[j]-(ll)sp[j]*(ll)sp[j]+(ll)sp[j]*(ll)sp[i];
}
long double prz(int x,int y){
ll b1=dp[x]-(ll)sp[x]*(ll)sp[x];
ll a1=sp[x];
ll b2=dp[y]-(ll)sp[y]*(ll)sp[y];
ll a2=sp[y];
if(a1==a2) return 1e18;
return (long double)(b2-b1)/(long double)(a1-a2);
}
void del(int i){
while(deq.size()>1){
int p=deq.front();
deq.pop_front();
int d=deq.front();
if(wz(p,i)>wz(d,i)){
deq.push_front(p);
break;
}
}
}
void dod(int i){
while(deq.size()>1){
int p=deq.back();
deq.pop_back();
int d=deq.back();
if(prz(p,i)==1e18){
if(dp[p]-(ll)sp[p]*(ll)sp[p]>=dp[i]-(ll)sp[i]*(ll)sp[i]){
deq.push_back(p);
return;
}
continue;
}else{
if(prz(p,i)>prz(d,i)){
deq.push_back(p);
break;
}
}
}
deq.push_back(i);
}
void solve(){
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>t[i];
sp[i]=sp[i-1]+t[i];
}
deq.push_back(0);
for(int j=1;j<=k;j++){
for(int i=1;i<=n;i++){
del(i);
dp2[i]=wz(deq.front(),i);
bb[i][j]=deq.front();
dod(i);
}
for(int i=1;i<=n;i++) dp[i]=dp2[i];
deq.clear();
}
cout<<dp[n]<<"\n";
int curr=n;
for(int i=k;i>=1;i--){
curr=bb[curr][i];
cout<<curr<<" ";
}
cout<<"\n";
}
int main(){
solve();
}
# | 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... |