Submission #41636

#TimeUsernameProblemLanguageResultExecution timeMemory
41636funcsrSplit the sequence (APIO14_sequence)C++14
39 / 100
2031 ms25784 KiB
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cassert>
#include <map>
using namespace std;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
//#define index(x, y) (int)(lower_bound(all(x), y) - x.begin())
#define index(xs, xe, y) (int)(lower_bound(xs, xe, y) - xs)
#define _1 first
#define _2 second
#define pb push_back
#define MOD 1000000007
#define INF (1LL<<60)
typedef pair<long long, long long> P;
P rel(long long p, long long q) {
  if (q < 0) p = -p, q = -q;
  return P(p, q);
}
inline P x_intersect(P a, P b) { return rel(b._2-a._2, a._1-b._1); }
inline bool ord(P a, P b) {
  // ap/aq < bp/bq
  return a._1*b._2 < a._2*b._1;
}

struct ConvexHullTrick {
  vector<P> ps;
  void add(long long x, long long y) {
    if (!ps.empty() && ps.back()._1 == x) {
      if (ps.back()._2 <= y) return;
      ps.pop_back();
    }
    P c = P(x, y);
    while (ps.size() >= 2) {
      P a = ps[ps.size()-2], b = ps[ps.size()-1];
      if (ord(x_intersect(b, c), x_intersect(a, c)) && ord(x_intersect(a, c), x_intersect(a, b))) break;
      ps.pop_back();
    }
    ps.pb(P(x, y));
  }
  P f(long long a) {
    if (ps.empty()) return P(-1, INF);
    P ret = P(INF, -1);
    for (P p : ps) {
      ret = min(ret, P(p._1*a+p._2, p._1));
    }
    return P(ret._2, ret._1);
  }
};

int N, K;
int B[10001];
long long dp[10001][202];
int pre[10001][202];

signed main() {
  cin >> N >> K;
  K++;
  rep(i, N) cin >> B[i+1];
  rep(i, N) B[i+1] += B[i];
  rep(i, N+1) rep(j, K+1) dp[i][j] = INF, pre[i][j] = -1;
  ConvexHullTrick cht;
  dp[0][0] = 0;
  cht.add(0, 0);
  rep(k, K) {
    ConvexHullTrick ncht;
    for (int x=1; x<=N; x++) {
      P p = cht.f(-2LL*B[x]);
      dp[x][k+1] = 1LL*B[x]*B[x] + p._2;
      pre[x][k+1] = min(index(B, B+N+1, p._1+1)-1, x-1);
      ncht.add(B[x], dp[x][k+1] + 1LL*B[x]*B[x]);
    }
    swap(cht, ncht);
  }
  long long m = (1LL*B[N]*B[N]-dp[N][K])/2LL;
  cout << m << "\n";
  int p = pre[N][K], k = K-1;
  vector<int> seq;
  while (k > 0) {
    seq.pb(p);
    p = pre[p][k];
    k--;
  }
  reverse(all(seq));
  for (int x : seq) cout << x << " "; 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...