제출 #285205

#제출 시각아이디문제언어결과실행 시간메모리
285205davooddkareshki수열 (APIO14_sequence)C++17
0 / 100
1531 ms33720 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define F first #define S second #define pii pair<int,int> #define mpr make_pair typedef long long ll; const int maxn = 1e4+10; const int mod = 1e9+7; const ll inf = 1e18+10; int n, K; int dp[maxn][210], up[maxn][210]; int ps[maxn], a[maxn]; void upd(int i, int k, int j) { if(j < 0 || j >= i) return; int sum = ps[i]-ps[j]; int val = dp[j][k-1] + sum*(sum-1)/2; if(val < dp[i][k]) { dp[i][k] = val; up[i][k] = j; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>> n >> K; K++; for(int i = 1; i <= n; i++) { cin>> a[i]; ps[i] = ps[i-1] + a[i]; } dp[0][0] = 0; for(int k = 1; k <= K; k++) { for(int i = 1; i <= n; i++) { dp[i][k] = inf; int hi = i, lo = 0; while(hi-lo > 1) { int tm = (hi + lo) >> 1; if(ps[i] - ps[tm] >= ps[i]/k) lo = tm; else hi = tm; } /// ps[i] - ps[lo] >= ps[i] / k for(int j = 0; j < i; j++) upd(i,k,j); /* upd(i,k,lo-2); upd(i,k,lo-1); upd(i,k,lo); upd(i,k,lo+1); upd(i,k,lo+2); */ } } ll ans = ps[n] * (ps[n] - 1) / 2; ans -= dp[n][K] * (dp[n][K]-1) / 2; int i = n, k = K; vector<int> out; while(k) { out.push_back(up[i][k]); i = up[i][k]; k--; } cout<< ans <<"\n"; for(auto x : out) cout<< x <<" "; } /* 7 3 4 1 3 4 0 2 3 */
#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...