제출 #892125

#제출 시각아이디문제언어결과실행 시간메모리
892125ShaShi수열 (APIO14_sequence)C++17
71 / 100
116 ms131072 KiB
#include <bits/stdc++.h> #define int long long #define F first #define S second #define kill(x) cout << x << endl, exit(0); #define all(x) x.begin(), x.end() #define pb push_back #define mp make_pair #define pii pair<int, int> #define endl "\n" using namespace std; typedef long long ll; typedef long double ld; const int MAXN = (int)1e5 + 70; const int K = 220; const int MOD = 998244353; const int INF = (int)3e18 + 7; int n, m, t, k, q, u, v, w, tmp, tmp2, tmp3, tmp4, tmp5, flag, ans; int arr[MAXN], ps[MAXN], opt[MAXN][K]; int dp[MAXN][K]; int cost(int x, int k, int s) { int res = ps[x]-ps[s-1]; return dp[s-1][k-1]+res*(res-1)/2; } #define mid ((l+r)>>1) void Divide(int ind, int l, int r, int s, int t) { if (l > r) return; for (int i=s; i<=min(mid, t); i++) { if (opt[mid][ind] == -1 || cost(mid, ind, opt[mid][ind]) > cost(mid, ind, i)) opt[mid][ind] = i; } dp[mid][ind] = cost(mid, ind, opt[mid][ind]); Divide(ind, l, mid-1, s, opt[mid][ind]); Divide(ind, mid+1, r, opt[mid][ind], t); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; k++; for (int i=1; i<=n; i++) { cin >> arr[i]; ps[i] = ps[i-1]+arr[i]; } for (int j=0; j<=k; j++) for (int i=0; i<=n; i++) dp[i][j] = INF, opt[i][j] = -1; dp[0][0] = 0; for (int i=1; i<=k; i++) Divide(i, 1, n, 1, n); cout << ps[n]*(ps[n]-1)/2 - dp[n][k] << endl; tmp = n; for (int i=0; i<k-1; i++) cout << (tmp = opt[tmp][k-i]-1) << " "; 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...