제출 #728919

#제출 시각아이디문제언어결과실행 시간메모리
728919hoainiem수열 (APIO14_sequence)C++14
71 / 100
328 ms131072 KiB
#include <bits/stdc++.h> #define fi first #define se second #define lc id<<1 #define rc id<<1^1 #define nmax 100008 const long long inf = 1e18; using namespace std; typedef pair<long long, long long> pii; long long n, k, row, a[nmax], f[nmax]; long long sum[nmax], dp[208][nmax], trace[208][nmax]; void dq(long long l, long long r, long long u, long long v){ if (l > r) return; long long mid = (l + r) >> 1, sp = -1; dp[row][mid] = inf; for (long long i = u; i <= mid && i <= v; i++) if (dp[row][mid] > dp[row - 1][i - 1] + sum[mid] - sum[i - 1] - 1LL * (f[mid] - f[i - 1]) * f[i - 1]){ dp[row][mid] = dp[row - 1][i - 1] + sum[mid] - sum[i - 1] - 1LL * (f[mid] - f[i - 1]) * f[i - 1]; sp = i; } trace[row][mid] = sp; dq(l, mid - 1, u, sp); dq(mid + 1, r, sp, v); } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (long long i = 1; i <= n; i++){ cin >> a[i]; f[i] = f[i - 1] + a[i]; sum[i] = sum[i - 1] + a[i] * f[i - 1]; dp[0][i] = inf; } k++; dp[0][0] = 0; for (row = 1; row <= k; row++) dq(row, n, row, n); cout << sum[n] - dp[k][n] << '\n'; for (long long j = k, i = n; j > 0; j--){ i = trace[j][i] - 1; if (i) cout << i << ' '; } 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...