제출 #49411

#제출 시각아이디문제언어결과실행 시간메모리
49411Benq수열 (APIO14_sequence)C++14
100 / 100
923 ms83196 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, ll> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
 
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
 
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
 
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
 
ll ans = 0;
int n,k;
ll num[MX], sum[MX];
array<ll,MX> dp, dp2;
int pre[202][MX];
 
ll eval(int y, int x) {
    return (sum[x]-sum[y])*(sum[x]-sum[y])+dp2[y];
}
 
ll bet(int x, int y) { 
    if (sum[y] == sum[x]) return 1e18;
    return (sum[y]*sum[y]+dp2[y]-sum[x]*sum[x]-dp2[x])/(2*sum[y]-2*sum[x]);
}

struct {
	int d[2*MX], opt[2*MX];
	int l = 0, r = -1;
	void clear() { l = 0, r = -1; }
	int front() { return d[l]; }
	int size() { return r-l+1; }
	int back() { return d[r]; }
	void pop_back() { r --; }
	void push_back(int x, int t) { 
		d[++r] = x; 
		if (size() > 1) opt[r-1] = t;
	}
	void pop_front() { l ++; }
	void push_front(int x) { d[--l] = x; }
	bool deleteFront(int x) {
		if (size() <= 1) return 0;
		return eval(d[l],x) >= eval(d[l+1],x);
	}
	bool deleteBack(int x) {
		if (size() <= 1) {
			int t = -1;
			if (size() == 1) t = bet(d[r],x);
			push_back(x,t);
			return 0;
		}
		int t = bet(d[r],x);
		if (opt[r-1] >= t) return 1;
		push_back(x,t);
		return 0;
	}
} bes;
 
void ins(int x) {
	while (bes.deleteBack(x)) bes.pop_back();
	// bes.pb(x);
}
 
void compute(int ind, int x) {
	while (bes.deleteFront(x)) bes.pop_front();
	dp[x] = eval(bes.front(),x);
	pre[ind][x] = bes.front();
}

void solve(int ind) {
    swap(dp,dp2);
    bes.clear();
    FOR(j,ind,n+1) {
        ins(j-1);
        compute(ind,j);
        // cout << "HI " << ind << " " << j << " " << dp2[0] << " " << dp[j] << "\n";
    }
    // cout << "----\n";
}
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k; k++;
    FOR(i,1,n+1) {
        // num[i] = rand() % 10000; 
        cin >> num[i];
        sum[i] = num[i]+sum[i-1];
    }
    FOR(i,1,n+1) dp[i] = 1e18;
    FOR(i,1,k+1) solve(i);
    int x = n;
    vi v;
    FORd(i,1,k+1) {
        x = pre[i][x];
        if (i > 1) v.pb(x);
    }
    reverse(all(v));
    cout << (sum[n]*sum[n]-dp[n])/2 << "\n";
    for (int i: v) cout << i << " ";
}
 
// read the question correctly (is y a vowel? what are the exact constraints?)
#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...