Submission #958687

#TimeUsernameProblemLanguageResultExecution timeMemory
958687vjudge1Split the sequence (APIO14_sequence)C++14
100 / 100
608 ms89040 KiB
#include<bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define int long long

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

struct LineContainer : vector<Line> {
	// (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, int tr) {
		auto z = insert(end(), {k, m, 0, tr}), 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));
	}
   int id=0;
	pair<int, int> query(ll x) {
		assert(!empty());
      while ((begin()+id)->p<x) ++id;
		return {(begin()+id)->k * x + (begin()+id)->m, (begin()+id)->tr};
	}
};

const int N=1e5+10;
int n, k, a[N], pf[N], f[2][N];
int32_t tr[N][210];
LineContainer cht;

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   cin >> n >> k;
   for (int i=1; i<=n; ++i) cin >> a[i];
   partial_sum(a, a+n+1, pf);
   for (int j=1; j<=k; ++j){
      cht.id=0;
      cht.add(pf[j], f[0][j]-pf[j]*pf[j], j);
      for (int i=j+1; i<=n; ++i){
         auto tmp=cht.query(pf[i]);
         tr[i][j]=tmp.second;
         f[1][i]=tmp.first;
         cht.add(pf[i], f[0][i]-pf[i]*pf[i], i);
      }
      cht.clear();
      memcpy(f[0], f[1], sizeof f[1]);
   }
   cout << f[0][n] << '\n';
   vector<int> ans;
   while (n){
      if (tr[n][k]) ans.push_back(tr[n][k]);
      n=tr[n][k];
      --k;
   }
   reverse(ans.begin(), ans.end());
   for (int i:ans) cout << i << ' ';
   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...