제출 #23253

#제출 시각아이디문제언어결과실행 시간메모리
23253Nurlykhan수열 (APIO14_sequence)C++14
22 / 100
129 ms4004 KiB
#include <bits/stdc++.h> #define pii pair<int, int> #define f first #define s second #define pb push_back #define mp make_pair #define ll long long #define ld long double #define sz(v) int(v.size()) #define all(v) v.begin(), v.end() using namespace std; const int N = (int) 100; const int M = (int) 2e6 + 10; const ll LINF = (ll) 1e18; const int INF = (int) 1e9 + 7; const double EPS = (double) 1e-9; int n, k; map<pair<int, pii>, ll> dp; int a[N]; int sum(int l, int r) { return a[r] - a[l - 1]; } int get_id(int l, int r) { int ans = l; for (int i = l; i < r; i++) { if (sum(l, ans) * 1ll * sum(ans + 1, r) < sum(l, i) * 1ll * sum(i + 1, r)) ans = i; } return ans; } ll calc(int l, int r, int k); ll f(int l, int r, int k, int i) { return calc(l, i, k - 1) + sum(l, i) * 1ll * sum(i + 1, r); } ll f1(int l, int r, int k, int i) { return calc(i + 1, r, k - 1) + sum(l, i) * 1ll * sum(i + 1, r); } ll calc(int l, int r, int k) { if (l > r) return 0; if (k == 0) return 0; if (dp.count(mp(k, mp(l, r)))) return dp[mp(k, mp(l, r))]; ll ans = 0; //ptr[l] = max(ptr[l], l); //ptr1[l] = max(ptr1[l], l); int ptr=l,ptr1=l; while (ptr + 1 < r && f(l, r, k, ptr) <= f(l, r, k, ptr + 1)) ptr++; while (ptr1 + 1 < r && f1(l, r, k, ptr1) <= f1(l, r, k, ptr1 + 1)) ptr1++; ans = max(f(l, r, k, ptr), f1(l, r, k, ptr1)); return dp[mp(k, mp(l, r))]=ans; } void print(int l, int r, int k) { if (k == 0) return; for (int i = l; i < r; i++) { for (int t = 0; t <= 0; t++) { if (calc(l, r, k) == calc(l, i, t) + calc(i + 1, r, k - 1 - t) + sum(l, i) * 1ll * sum(i + 1, r)) { cout << i << " "; print(l, i, t); print(i + 1, r, k - 1 - t); return; } } for (int t = k - 1; t < k; t++) { if (calc(l, r, k) == calc(l, i, t) + calc(i + 1, r, k - 1 - t) + sum(l, i) * 1ll * sum(i + 1, r)) { cout << i << " "; print(l, i, t); print(i + 1, r, k - 1 - t); return; } } } } int main() { #define fn "balls" #ifdef witch freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #else // freopen(fn".in", "r", stdin); // freopen(fn".out", "w", stdout); #endif cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] += a[i - 1]; } cout << calc(1, n, k) << endl; print(1, n, k); 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...