제출 #734213

#제출 시각아이디문제언어결과실행 시간메모리
734213Alihan_8수열 (APIO14_sequence)C++17
28 / 100
2086 ms5644 KiB
#include <bits/stdc++.h> using namespace std; #define all(x) x.begin(), x.end() #define pb push_back #define ln '\n' #define int long long template <class _T> bool chmin(_T &x, const _T &y){ bool flag = false; if ( x > y ){ x = y; flag |= true; } return flag; } template <class _T> bool chmax(_T &x, const _T &y){ bool flag = false; if ( x < y ){ x = y; flag |= true; } return flag; } const int N = 1e5 + 1, inf = 1e15 + 1; int dp[2][N], par[202][N]; signed main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; vector <int> p(n), pref(n + 1); for ( auto &i: p ) cin >> i; for ( int i = 0; i < n; i++ ){ pref[i + 1] = pref[i] + p[i]; } k++; auto cost = [&](int l, int r){ return (pref.back() - pref[r]) * (pref[r] - pref[l - 1]); }; memset(dp[1], 0, sizeof(dp[1])); for ( int i = 0; i < k; i++ ){ swap(dp[1], dp[0]); for ( int j = 1; j <= n; j++ ){ for ( int q = 0; q < j; q++ ){ if ( chmax(dp[1][j], dp[0][q] + cost(q + 1, j)) ){ par[i + 1][j] = q; } } } } cout << dp[1][n] << ln; vector <int> res; int it = n; while ( k > 0 ){ it = par[k--][it]; if ( it ) res.pb(it); } reverse(all(res)); for ( auto i: res ){ cout << i << ' '; } cout << '\n'; } /* 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...