제출 #377905

#제출 시각아이디문제언어결과실행 시간메모리
377905ritul_kr_singhSplit the sequence (APIO14_sequence)C++17
0 / 100
13 ms3436 KiB
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'

vector<array<int, 3>> hull[200+1];

int f(int x, int i, int j){
	if(j<0) j += hull[i].size();
	return hull[i][j][0]*x + hull[i][j][1] - x*x;
}

bool bad(int m, int c, int i){
	int x1 = c-hull[i].back()[1];
	int d1 = hull[i].back()[0]-m;
	if(d1<0) d1 *= -1, x1 *= -1;

	c = hull[i][hull[i].size()-2][1], m = hull[i][hull[i].size()-2][0];

	int x2 = c-hull[i].back()[1];
	int d2 = hull[i].back()[0]-m;
	if(d2<0) d2 *= -1, x2 *= -1;

	return x1*d2 <= x2*d1;
}

signed main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int n, k; cin >> n >> k;
    int a[n], ans = 0, ind = 0;
    for(int &i : a) cin >> i;
    for(int i=n-2; i>=0; --i) a[i] += a[i+1];

	hull[0].push_back({a[0], 0});
	int p[n][k];

	for(int i=1; i<n; ++i){
		for(int j=k-1; j>=0; --j){
			if(hull[j].empty()) continue;
			while(hull[j].size() > 1 and f(a[i], j, -2) >= f(a[i], j, -1)) hull[j].pop_back();
			int dp = f(a[i], j, -1);
			p[i][j] = hull[j].back()[2];

			int m = a[i];

			while(!hull[j+1].empty()){
				if(hull[j+1].back()[0]==m){
					dp = max(dp, hull[j+1].back()[1]);
					hull[j+1].pop_back();
				}else if(hull[j+1].size() > 1 and bad(m, dp, j+1)){
					hull[j+1].pop_back();
				}else break;
			}
			hull[j+1].push_back({m, dp, i});
			if(dp > ans){
				ans = dp;
				ind = i;
			}
		}
	}
	vector<int> res = {ind};
	for(int j=k-1; j>=0; --j){
		res.push_back(p[ind][j]);
		ind = res.back();
	}
	cout << ans nl;
	for(int i=res.size()-2; i>=0; --i) cout << res[i] << ' ';
}
#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...