Submission #670133

#TimeUsernameProblemLanguageResultExecution timeMemory
670133NothingXDSplit the sequence (APIO14_sequence)C++17
100 / 100
511 ms84224 KiB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
/*typedef __uint128_t L;
struct FastMod {
  ull b, m;
  FastMod(ull b) : b(b), m(ull((L(1) << 64) / b)) {}
  ull reduce(ull a) {
    ull q = (ull)((L(m) * a) >> 64);
    ull r = a - q * b; // can be proven that 0 <= r < 2*b
    return r >= b ? r - b : r;
  }
};
FastMod FM(2);*/
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

struct CHT{
	static const ll inf = 1e18;
	int l, r;
	vector<pair<ll,pair<pll,ll>>> a;
	CHT(){};
	CHT(int x){
		a.resize(x);
		l = r = 0;
	}
	ll insec(pll x, pll y){
		if (x.F == y.F) return (x.S <= y.S? -inf: inf);
		return (x.S - y.S) / (y.F - x.F) + ((x.S - y.S) % (y.F - x.F) > 0);
	}
	void add(pair<pll,ll> x){
		while(l < r && insec(a[r-1].S.F, x.F) <= a[r-1].F) r--;
		if (l == r){
			a[r++] = {-inf, x};
		}
		else{
			a[r] = {insec(a[r-1].S.F, x.F), x};
			r++;
		}
	}
	int get(ll x){
		while(l + 1 < r && a[l+1].F <= x) l++;
		return a[l].S.S;
	}
};

const int maxn = 1e5 + 10;
const int maxk = 200 + 10;

int n, k, a[maxn], flg[maxk][maxn], part[maxn];
ll dp[2][maxn];

int main(){
	ios_base::sync_with_stdio(false); cin.tie(0);

	cin >> n >> k;

	for (int i = 1; i <= n; i++) cin >> a[i];

	for (int i = 1; i <= n; i++) part[i] = part[i-1] + a[i];

	for (int i = 1; i <= k; i++){
		CHT cht(n+1);
		cht.add({{0, 0}, 0});
		int PTR = (i&1);
		int PRV = 1 - PTR;
		for (int j = 1; j <= n; j++){
			int ptr = cht.get(part[j]);
			dp[PTR][j] = dp[PRV][ptr] + 1ll * (part[j] - part[ptr]) * part[ptr];
			flg[i][j] = ptr;
			cht.add({{part[j], dp[PRV][j] - 1ll * part[j] * part[j]}, j});
		}
	}

	cout << dp[k&1][n] << '\n';

	int ptr = n;
	int tmp = k;
	while(tmp){
		cout << flg[tmp][ptr] << ' ';
		ptr = flg[tmp][ptr];
		tmp--;
	}
	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...