제출 #981782

#제출 시각아이디문제언어결과실행 시간메모리
981782Halym2007수열 (APIO14_sequence)C++17
100 / 100
694 ms84564 KiB
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
#define ll long long
#define sz size()
#define pb push_back
ll p[N], a[N], n, k, dp[N];
int par[N][205];
//CHT means convex hull trick
struct CHT {
	ll m, c;
	int idx;
	ll get (ll x) {	
		return m * x + c;
	}
	long double intersect (CHT x) {
		return (long double)(x.c - c) / (long double)(m - x.m);
	}
};

int main () {
//	freopen ("input.txt", "r", stdin);
	cin >> n >> k;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		p[i] = p[i - 1] + a[i];
	}
	for (int i = 1; i <= k; ++i) {
		deque <CHT> d;
		d.pb ({p[i], dp[i] - p[i] * p[i], i});
		for (int j = i + 1; j <= n; ++j) {
			while ((int)d.sz >= 2 and d[0].get (p[j]) <= d[1].get (p[j])) d.pop_front();
			ll aa = d[0].get (p[j]);
			CHT tr = {p[j], dp[j] - p[j] * p[j],  j};
			par[j][i] = d[0].idx;
			while ((int)d.sz >= 2 and tr.intersect(d[(int)d.sz - 2]) <= d.back().intersect(d[(int)d.sz - 2])) d.pop_back();
			d.pb (tr);
			dp[j] = aa;
		}
		
	}
	cout << dp[n] << "\n";
	int idx = par[n][k];
	int val = k;
	while (idx) {
		val--;
		cout << idx << " ";
		idx = par[idx][val];
	}
}
#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...