Submission #1118118

#TimeUsernameProblemLanguageResultExecution timeMemory
1118118uroskSplit the sequence (APIO14_sequence)C++14
50 / 100
164 ms25052 KiB
#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 = 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 = 10005;
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;
    	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--;
    			if(t<0||t>=si(v)) {
    				while(1) here;
    			}
    			dp[j][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]);
    		ll k1 = ps[i],m1 = -ps[i]*ps[i]+dp[j-1][i];
    		while(si(v)) {
    			auto it = v.back();
    			ll k2 = it[1],m2 = it[2];
    			if(k1==k2) {
    				if(m1>m2) {
    					while(v.empty()) here;
    					v.popb();
    					continue;
    				}
    				break;
    			}
    			ld cx = (m2-m1)*1.0/(k1-k2);
    			if(cx<=it[0]) {
    				while(v.empty()) here;
    				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][n]<<endl;
    vector<ll> v;
    for(ll i = k;i>=1;i--) {
    	n = opt[i][n];
    	v.pb(n);
    }
    for(ll x : v) cout<<x<< " ";
    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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...