제출 #41651

#제출 시각아이디문제언어결과실행 시간메모리
41651funcsr수열 (APIO14_sequence)C++14
100 / 100
1902 ms83308 KiB
#pragma GCC optimize ("-O2") #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; inline P rel(long long p, long long q) { if (q < 0) p = -p, q = -q; return P(p, q); } inline P x_intersect(const P &a, const P &b) { return rel(b._2-a._2, a._1-b._1); } inline bool ord(const P &a, const P &b) { // ap/aq < bp/bq //return (__int128)a._1*b._2 < (__int128)a._2*b._1; return a._1*b._2 < a._2*b._1; } pair<P, int> ps[100001]; struct ConvexHullTrick { int sz = 0, h = 0; void add(long long x, long long y, int attr) { if (sz && ps[sz-1]._1._1 == x) { if (ps[sz-1]._1._2 < y) return; sz--; } P c = P(x, y); while (sz >= 2) { P a = ps[sz-2]._1, b = ps[sz-1]._1; P p = x_intersect(a, c); if (ord(x_intersect(b, c), p) && ord(p, x_intersect(a, b))) break; sz--; } //h = max(0, min(h, (int)ps.size()-1)); ps[sz++] = make_pair(P(x, y), attr); } P f(long long a) { assert(sz); //if (sz == 0) return P(-1, INF); while (h+1 < sz) { if (ord(P(a, 1), x_intersect(ps[h]._1, ps[h+1]._1))) h++; else break; } return P(ps[h]._2, a*ps[h]._1._1+ps[h]._1._2); } void clear() { h = 0, sz = 0; } }; int N, K; int B[100001]; int pre[100001][202]; long long dp[100001]; ConvexHullTrick cht; 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) pre[i][j] = -1; cht.add(0, 0, 0); rep(k, K) { rep(i, N+1) dp[i] = INF; for (int x=k+1; x<=N; x++) { P p = cht.f(-2LL*B[x]); dp[x] = 1LL*B[x]*B[x] + p._2; pre[x][k+1] = min((int)p._1, x-1); } cht.clear(); for (int x=k+1; x<=N; x++) cht.add(B[x], dp[x] + 1LL*B[x]*B[x], x); } long long m = (1LL*B[N]*B[N]-dp[N])/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...