제출 #302471

#제출 시각아이디문제언어결과실행 시간메모리
302471fishy15수열 (APIO14_sequence)C++17
60 / 100
2035 ms33272 KiB
#include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <array> #include <algorithm> #include <utility> #include <map> #include <queue> #include <set> #include <cmath> #include <cstdio> #include <cstring> #define ll long long #define ld long double #define eps 1e-8 #define MOD 1000000007 #define INF 0x3f3f3f3f #define INFLL 0x3f3f3f3f3f3f3f3f // change if necessary #define MAXN 100010 using namespace std; int n, k; int nums[MAXN]; ll psum[MAXN]; pair<ll, int> dp[210][MAXN]; void solve(int i, int l, int r, int opt_l, int opt_r) { if (l > r) return; int m = (l + r) / 2; ll max_prod = 0; int pos = opt_l; for (int j = opt_l; j <= max(m, opt_r); j++) { ll before = psum[j + 1]; ll after = psum[m + 1] - psum[j + 1]; ll prod = before * after + dp[i - 1][j].first; if (prod > max_prod) { max_prod = prod; pos = j; } } dp[i][m] = {max_prod, pos}; solve(i, l, m - 1, opt_l, pos); solve(i, m + 1, r, pos, opt_r); } int main() { /* cin.tie(0)->sync_with_stdio(0); */ cin >> n >> k; for (int i = 0; i < n; i++) { cin >> nums[i]; } for (int i = 1; i <= n; i++) { psum[i] = psum[i - 1] + nums[i - 1]; } for (int i = 1; i <= k; i++) { solve(i, 0, n - 1, 0, n - 1); } pair<ll, int> ans = {0, 0}; for (int i = 0; i < n; i++) { ans = max(ans, dp[k][i]); } cout << ans.first << '\n'; vector<int> v; for (int i = k - 1; i >= 0; i--) { v.push_back(ans.second + 1); ans = dp[i][ans.second]; } reverse(v.begin(), v.end()); for (int i : v) { cout << i << ' '; } cout << '\n'; 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...