제출 #989656

#제출 시각아이디문제언어결과실행 시간메모리
989656vjudge1수열 (APIO14_sequence)C++17
0 / 100
22 ms21596 KiB
#include<bits/stdc++.h>
#define pll pair<ll, ll>
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ar array
#define vi vector
#define bit(i,x) ((x >> i) & 1ll)
#define all(x) x.begin(),x.end()
#define low(l,r,x) x.begin()+l,x.begin()+r 
#define len(l,r) (r-l+1)
#define PI acos(-1.0) 
template <class T> void setmin(T &a, T b) { if(b < a) a = b; }
template <class T> void setmax(T &a, T b) { if(b > a) a = b; }

const long long mod = 998244353, N =  2e5, INF = 1e18, Log = 19, MASK = 1 << 19;
using namespace std;
 
#define int long long


ll pfs[N+5] , perf[N+5];
ll n , k;
ll dp[205][N+5];
ll trace[205][N+5];


ll C(ll i , ll j) {
    ll k = pfs[j] - pfs[i-1];
    ll res = k*k;
    res -= (perf[j] - perf[i-1]);
    return res;
}

void compute(ll x,ll l,ll r, ll optl, ll optr) {
    if(l > r) return;
    ll mid = (l+r) >> 1;
    pll best = {INF,-1};
    for(int i = optl; i <= min(mid,optr);i++) {
        best = min(best, {dp[x-1][i-1] + C(i,mid),i});
    }
    if(best.se == -1) return;
    dp[x][mid] = best.fi;
    trace[x][mid] = best.se-1;
    int opt = best.se;
    compute(x,l,mid-1,optl,opt);
    compute(x,mid+1,r,opt,optr);
}



void kazuha() {
    cin >> n >> k;
    ll a[n+5];
    pfs[0] = 0;
    for(int i = 1; i <= n;i++) {
        cin >> a[i];
        pfs[i] = pfs[i-1] + a[i];
        perf[i] = perf[i-1] + a[i]*a[i];
    }
    ll ans = pfs[n]*pfs[n] - perf[n];
    ans /= 2;
    for(int i = 0; i <= k+1;i++) {
        dp[i][0] = 0;
        for(int j = 1; j <= n;j++) {
            dp[i][j] = INF;
        }
    }
    for(int i = 1; i <= k+1;i++) {
        compute(i,1,n,1,n);
    }
    ans = ans - dp[k+1][n]/2;
    ll i = k+1, j = n;
    while(i > 1 && j > 1) {
        ll a = trace[i][j];
        cout << a << " ";
        i--;
        j = a;
    }
}
 
 
 
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // freopen("permutation.cpp", "r", stdin);
    // freopen("permutation.cpp", "w", stdout); 
    long long mitsu = 1;
    // cin >> mitsu;
    for(long long seele = 0; seele < mitsu;seele++) {
        // cout << "Case " << seele << " :"; 
        kazuha();
    }
}
#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...