This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include <bits/stdc++.h>
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define eb emplace_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}
#define si(a) (ll)(a.size())
using namespace std;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pld = pair<ld,ld>;
#define mod 1
ll add(ll x,ll y){
x+=y;
if(x<0){
x%=mod;
x+=mod;
}else{
if(x>=mod) x%=mod;
}
return x;
}
ll mul(ll a,ll b){
a = add(a,0);
b = add(b,0);
ll ans = (a*b)%mod;
if(ans<0) ans+=mod;
return ans;
}
const ll maxn = 100005;
const ll maxk = 205;
const ll maxx = 1000005;
const ll mx = 40000000000000000;
ll n,k;
ll a[maxn];
ll ps[maxn];
ll dp[maxk][maxn];
int opt[maxk][maxn];
void tc(){
cin >> n >> k;
for(ll i = 1;i<=n;i++) cin >> a[i];
for(ll i = 1;i<=n;i++) ps[i] = ps[i-1] + a[i];
for(ll j = 1;j<=k;j++) {
vector<array<ld,4> > v;
ll t = 0;
bool e = j&1;
bool f = 1-e;
for(ll i = 1;i<=n;i++) dp[e][i] = 0;
for(ll i = j;i<=n;i++) {
if(si(v)) {
t = min(t,si(v)-1);
t = max(t,0LL);
while(t+1<si(v)&&ps[i]>=v[t+1][0]) t++;
while(t>=1&&ps[i]<v[t][0]) t--;
dp[e][i] = v[t][1]*ps[i]+v[t][2];
opt[j][i] = v[t][3];
}
//lin f(-ps[i]*ps[i]+dp[j-1][i],ps[i]);
ld k1 = ps[i],m1 = -ps[i]*ps[i]+dp[f][i];
while(si(v)) {
auto it = v.back();
ll k2 = it[1],m2 = it[2];
if(k1==k2) {
if(m1>m2) {
v.popb();
continue;
}
break;
}
ld cx = (m2-m1)*1.0/(k1-k2);
if(cx<=it[0]) {
v.popb();
}else {
v.pb({cx*1.0,1.0*k1,1.0*m1,i*1.0});
break;
}
}
if(v.empty()) v.pb({-mx*1.0,1.0*k1,1.0*m1,i*1.0});
}
//ceri(dp[j],1,n);
}
cout<<dp[k&1][n]<<endl;
for(ll i = k;i>=1;i--) {
n = opt[i][n];
cout<<n<< " ";
}
cout<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
int t; t = 1;
while(t--){
tc();
}
return (0-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... |