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;
typedef long long ll;
#define MAXN 100005
#define MAXK 205
const ll INF = 1e18;
//ll dp[MAXK][MAXN];
ll dp[2][MAXN];
ll a[MAXN];
int opt[MAXK][MAXN];
deque <int> q;//deque <line> q;
//struct line {ll n;ll k;int id;};
double presekx(int x,int y,int k)
{
if(a[x]==a[y])return INF;
return 1.00*((dp[k%2][x]-a[x]*a[x])-(dp[k%2][y]-a[y]*a[y]))/(a[y]-a[x]);
//if(a.k==b.k)return INF;
//return 1.00*(a.n-b.n)/(b.k-a.k);
}
int next_front()//line next_front()
{
int a = q.front();//line a = q.front();
q.pop_front();
int b = q.front();//line b = q.front();
q.push_front(a);
return b;
}
int prev_back()//line prev_back()
{
int a = q.back();//line a = q.back();
q.pop_back();
int b = q.back();//line b = q.back();
q.push_back(a);
return b;
}
ll eval(int l,int k,ll x)
{
return (a[l])*x+(dp[k%2][l]-a[l]*a[l]);//return l.k*x+l.n;
}
int main()
{
ios_base::sync_with_stdio(false);
int n,k;
cin >> n >> k;
for(int i = 1; i <= n; i++)cin >> a[i],a[i]+=a[i-1];
for(int i = 1; i <= k; i++)
{
q.clear();
for(int j = i; j <= n; j++)
{
while(1)
{
if(q.size()<=1)break;
if(eval(q.front(),i-1,a[j])<=eval(next_front(),i-1,a[j])){q.pop_front();continue;}
break;
}
if(q.size())
{
dp[i%2][j]=eval(q.front(),i-1,a[j]);
opt[i][j]=q.front();//opt[i][j]=q.front().id;
}
int add;//line add;
//add.k=a[j];
//add.n=-a[j]*a[j]+dp[i-1][j];
add=j;//add.id=j;
while(1)
{
if(q.size()<=1)break;
if(a[q.back()]==a[add]){q.pop_back();continue;}//if(q.back().k==add.k){q.pop_back();continue;}
if(a[q.back()]==a[prev_back()]){q.pop_back();continue;}//if(q.back().k==prev_back().k){q.pop_back();continue;}
if(presekx(prev_back(),q.back(),i-1)>=presekx(q.back(),add,i-1)){q.pop_back();continue;}//if(presekx(prev_back(),q.back())>=presekx(q.back(),add)){q.pop_back();continue;}
break;
}
q.push_back(add);
}
}
cout << dp[k%2][n] << "\n";
int now = n;
while(k > 0)
{
cout << opt[k][now] << " ";
now=opt[k][now];
k--;
}
return 0;
}
# | 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... |