Submission #948841

#TimeUsernameProblemLanguageResultExecution timeMemory
948841happypotato수열 (APIO14_sequence)C++17
50 / 100
2055 ms131072 KiB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define int long long
#define ll long long

struct Line {
	mutable ll k, m, p;
	bool operator<(const Line& o) const { return k < o.k; }
	bool operator<(ll x) const { return p < x; }
};

struct LineContainer : multiset<Line, less<>> {
	// (for doubles, use inf = 1/.0, div(a,b) = a/b)
	static const ll inf = LLONG_MAX;
	ll div(ll a, ll b) { // floored division
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) return x->p = inf, 0;
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m) {
		auto z = insert({k, m, 0}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p)
			isect(x, erase(y));
	}
	ll query(ll x) {
		assert(!empty());
		auto l = *lower_bound(x);
		return l.k * x + l.m;
	}
};

/*
dp[x][k] = min(dp[p][k-1] + (ps[x] - ps[p])^2)
dp[x][k] = ps[x]^2 + min(-2(ps[x])(ps[p]) + (ps[p]^2 + dp[p][k-1]))
dp[x][k] = ps[x]^2 + max(2(ps[x])(ps[p]) - (ps[p]^2 + dp[p][k-1]))
*/
const int MAX = 2e18;
const int mxN = 1e5 + 1, mxK = 202;
int dp[mxK][mxN];
int a[mxN], ps[mxN];
void solve() {
	int n, k;
	cin >> n >> k;
	ps[0] = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		ps[i] = ps[i - 1] + a[i];
	}
	for (int i = 0; i <= n; i++) {
		dp[1][i] = ps[i] * ps[i];
	}
	LineContainer LC;
	for (int i = 2; i <= k + 1; i++) {
		LC.clear();
		LC.add(2LL * ps[i - 1], -(ps[i - 1] * ps[i - 1] + dp[i - 1][i - 1]));
		for (int p = i; p <= n; p++) {
			dp[i][p] = (ps[p] * ps[p]) - LC.query(ps[p]);
			LC.add(2LL * ps[p], -(ps[p] * ps[p] + dp[i - 1][p]));
		}
	}

	int ans = ((ps[n] * ps[n]) - dp[k + 1][n]) / 2;
	vector<int> res;
	int cnt = k + 1, prev = n;
	for (int i = n - 1; cnt > 1 && i >= 1; i--) {
		if (dp[cnt - 1][i] + (ps[prev] - ps[i]) * (ps[prev] - ps[i]) == dp[cnt][prev]) {
			res.push_back(i); cnt--; prev = i;
		}
	}
	sort(res.begin(), res.end());
	cout << ans << '\n';
	for (int x : res) cout << x << ' ';
	cout << '\n';
}
int32_t main() {
	ios::sync_with_stdio(0); cin.tie(0);
	solve();
}
#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...