Submission #47907

#TimeUsernameProblemLanguageResultExecution timeMemory
47907square1001Split the sequence (APIO14_sequence)C++14
0 / 100
21 ms4512 KiB
#include <iostream>
#include <algorithm>
using namespace std;
const long long inf = 1LL << 60;
int n, m, c, s[100009], d[100009], p[209][100009]; long long dp[100009], ndp[100009];
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n; ++i) {
		cin >> s[i + 1];
		s[i + 1] += s[i];
	}
	fill(dp + 1, dp + n + 1, inf);
	for (int i = 0; i <= m; ++i) {
		for (int j = 0; j <= n; ++j) {
			while (c >= 2 && (s[j] - s[d[c - 1]]) * (dp[d[c - 1]] - dp[d[c - 2]]) >= (s[d[c - 1]] - s[d[c - 2]]) * (dp[j] - dp[d[c - 1]])) {
				c--;
			}
			d[c++] = j;
		}
		int ptr = 0;
		for (int j = 0; j <= n; ++j) {
			while (ptr + 1 != c && dp[d[ptr]] - 2 * s[j] * s[d[ptr]] > dp[d[ptr + 1]] - 2 * s[j] * s[d[ptr + 1]]) ptr++;
			ndp[j] = dp[d[ptr]] + 2LL * (s[j] - s[d[ptr]]) * s[j];
			p[i][j] = d[ptr];
		}
		copy(ndp, ndp + n + 1, dp);
	}
	cout << s[n] * s[n] - dp[n] / 2 << '\n';
	int pos = n;
	for (int i = m; i >= 1; --i) {
		pos = p[i][pos];
		cout << pos << (i != 1 ? ' ' : '\n');
	}
	return 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...