제출 #1213128

#제출 시각아이디문제언어결과실행 시간메모리
1213128Tob수열 (APIO14_sequence)C++20
100 / 100
989 ms90024 KiB
#include <bits/stdc++.h>
 
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
 
using namespace std;
 
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <__int128, __int128> pdd;
 
const int N = 1e5 + 7, K = 207;
 
int n, k;
ll a[N], ps[N];
int gt[N][K];
ll dp[N][2];
 
int ccw(pdd x, pdd y, pdd z) {return x.F*(y.S-z.S)+y.F*(z.S-x.S)+z.F*(x.S-y.S) >= 0;}
ll f(ll x, pii y) {return x*y.F+y.S;}
 
int main () {
	FIO;
	cin >> n >> k;
	
	for (int i = 0; i < n; i++) cin >> a[i], ps[i+1] = ps[i] + a[i];
	
	for (int i = 1; i <= k; i++) {
		int cur = 0;
		vector <pair <pii, int> > v;
		for (int j = 0; j < n; j++) {
			pii p = {ps[j], -ps[j]*ps[j]+dp[j][i%2==0]};
			while (v.size() > 1 && ccw(v[v.size()-2].F, v.back().F, p)) v.pop_back();
			if (j) cur = min(cur, (int)v.size()-1);
			v.pb({p, j});
			while (cur+1 < v.size() && f(ps[j+1], v[cur].F) <= f(ps[j+1], v[cur+1].F)) cur++;
			dp[j+1][i%2] = f(ps[j+1], v[cur].F);
			gt[j+1][i] = v[cur].S;
		}
	}
	cout << dp[n][k%2] << "\n";
	vector <int> res;
	int x = n;
	for (int i = k; i; i--) res.pb(x = gt[x][i]);
	reverse(all(res));
	for (int y : res) cout << y << " ";
 
	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...