Submission #384056

#TimeUsernameProblemLanguageResultExecution timeMemory
384056Drew_Split the sequence (APIO14_sequence)C++14
100 / 100
1788 ms82636 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

#define ll long long
#define pb push_back
#define ii pair<int, int>
#define f1 first
#define s2 second

const int MAX = 1e5 + 1;
const ll inf = 1e18;

int n;
ll pfx[MAX];
ll dp[2][MAX];

int par[201][MAX];
inline void rc(int k, int l, int r, int optL, int optR)
{
	queue<pair<ii, ii>> q;
	q.push({{l, r}, {optL, optR}});

	while (!q.empty())
	{
		l = q.front().f1.f1;
		r = q.front().f1.s2;
		optL = q.front().s2.f1;
		optR = q.front().s2.s2;
		q.pop();

		int mid = (l + r) / 2;
		ll optVal = -inf, optIdx = -1;

		for (int i = optL; i <= mid && i <= optR; ++i)
		{
			//segment [i, n], cut at mid
			ll v = dp[(k+1)&1][i-1]	+ (pfx[mid] - pfx[i-1]) * (pfx[n] - pfx[mid]);
			if (v > optVal)
			{
				optVal = v;
				optIdx = i;
			}
		}

		dp[k&1][mid] = optVal;
		par[k][mid] = optIdx;

		if (l < r)
		{
			q.push({{l, mid-1}, {optL, optIdx}});
			q.push({{mid+1, r}, {optIdx, optR}});
		}
	}
}

int main()
{
	ios :: sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int k;
	cin >> n >> k;

	for (int i = 1; i <= n; ++i)
		cin >> pfx[i], pfx[i] += pfx[i-1];

	for (int i = 1; i <= n; ++i)
		dp[0][i] = 0;

	for (int i = 1; i <= k; ++i)
		rc(i, i, n, i, n);

	ll res = -inf, idx = -1;
	for (int i = 1; i <= n; ++i)
	{
		if (res < dp[k&1][i])
			res = dp[k&1][i], idx = i;
	}
	
	vector<int> cut;

	cut.pb(idx);
	for (int j = k; j >= 2; --j)
		cut.pb(idx = par[j][idx] - 1);

	cout << res << '\n';
	for (int i = k-1; i >= 0; --i)
	{
		cout << cut[i];
		if (i) cout << " ";
	}
	cout << '\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...