제출 #875248

#제출 시각아이디문제언어결과실행 시간메모리
875248NintsiChkhaidzeSplit the sequence (APIO14_sequence)C++17
50 / 100
69 ms71596 KiB
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define s second
#define f first
#define left t[id][h].l,l,(l + r)/2
#define right t[id][h].r,(l + r)/2 + 1,r
#define pii pair <int,int>
#define int ll
using namespace std;
 
const int N = 1e3 + 3,inf = 1e8;
 
int p[N],lst[N][203];
ll dp[N][203];
int cnt[203];

struct {
	int k,b,l = 0,r = 0,vis=0;
} t[203][4*N];

ll f(int k,int b,int x){
	return x * k + b;
}
 
void upd(int id,int h,int l,int r,int K,int B,int idx){
 
	if (!t[id][h].vis){
		t[id][h].vis = idx;
		t[id][h].k = K;
		t[id][h].b = B;
		return;
	}
 
	int mid = (l + r)/2;
	if (f(t[id][h].k,t[id][h].b,mid) < f(K,B,mid)) {
		swap(t[id][h].k,K);
		swap(t[id][h].b,B);
		swap(t[id][h].vis,idx);
	}

	if (l == r) return;
	if (!t[id][h].l) t[id][h].l = ++cnt[id];
	if (!t[id][h].r) t[id][h].r = ++cnt[id];
 
	if (f(t[id][h].k,t[id][h].b,l) > f(K,B,l)){
		upd(id,right,K,B,idx);
	}else{
		upd(id,left,K,B,idx);
	}
}
 
pii get(int id,int h,int l,int r,int x){
	pii cur = {-1e9,-1};
	if (t[id][h].vis) cur = {f(t[id][h].k,t[id][h].b,x - 1),t[id][h].vis};
	if (l == r) return cur;
 
	pair <ll,int> res = {-1e9,-1};
	if (x > (l + r)/2) {
		if (t[id][h].r) res = get(id,right,x);
	}else{
		if (t[id][h].l) res = get(id,left,x);
	}
 
	if (res.f > cur.f) return res;
	return cur;
}
signed main() {
    ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
 
	int n,k;
	cin>>n>>k;
	
	for (int i = 1; i <= n; i++){
		cin >> p[i];
		p[i] += p[i - 1];
	}
	
	for (int r = 0; r <= n + 1; r++)
		for (int kk = 1; kk <= k; kk++)
			dp[r][kk] = -1;
 
	for (int i = 1; i <= k; i++)
		cnt[i] = 1;
 
	for (int i = 2; i <= n; i++){
		for (int j = 1; j <= min(k,i - 1); j++){
			if (j == 1){
				int cur = (p[n] - p[i - 1])*p[i - 1];
				if (cur > dp[i][j]){
					dp[i][j] = cur;
					lst[i][j] = i;
				}
				continue;
			}
 
			int s1 = p[n] - p[i - 1];
			pii res = get(j - 1,1,1,inf,s1+1);
			if (res.s == -1) continue;
			dp[i][j] = res.f + s1*p[i - 1];
			lst[i][j] = res.s;
		}
 
		for (int j = 1; j <= min(k,i - 1); j++){
			if (dp[i][j] != -1) {
				upd(j,1,1,inf,-p[i - 1],dp[i][j],i); 
			}
		}
	}
 
	int id=n;
	for (int i = 1; i <= n; i++){
		if (dp[i][k] > dp[id][k]) id = i;
	}
	cout<<dp[id][k]<<endl;
	
	vector <int> ans;
	for (int i = k; i >= 1; i--){
		ans.pb(id);
		id = lst[id][i];
	}
	
	for (int i = k - 1; i >= 0; i--)
		cout<<ans[i] - 1<<" ";
} 
#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...