제출 #1047164

#제출 시각아이디문제언어결과실행 시간메모리
1047164Sharky수열 (APIO14_sequence)C++17
0 / 100
15 ms4188 KiB
#include <bits/stdc++.h> using namespace std; #define int long long using db = long double; const int maxn = 1E5+5; int n, K, l = 1, r = 1, a[maxn], p[maxn], Q[maxn]; vector<int> dp, new_dp; bool m(int j, int k, int i) { return ((dp[k] - dp[j]) * (p[i] - p[k])) <= ((dp[i] - dp[k]) * (p[k] - p[j])); } bool idk(int j, int k, int i) { return (p[n] - p[i]) * (p[k] - p[j]) <= (dp[k] - dp[j]); } int32_t main() { cin >> n >> K; new_dp.assign(n+1, 0); dp.assign(n+1, 0); for (int i = 1; i <= n; i++) { cin >> a[i]; p[i] = p[i-1] + a[i]; } for (int i = 1; i <= n; i++) { dp[i] = p[i] * (p[n] - p[i]); } for (int kk = 2; kk <= K; kk++) { for (int i = 1; i <= n; i++) Q[i] = 0; for (int i = n; i >= 1; i--) new_dp[i] = dp[i]; l = r = 1; for (int i = 1; i < kk; i++) Q[r++] = i; --r; for (int i = kk; i <= n; i++) { while (r - l >= 1 && idk(Q[l], Q[l+1], i)) l++; if (l <= r) new_dp[i] = dp[Q[l]] + (p[i] - p[Q[l]]) * (p[n] - p[i]); while (r - l >= 1 && m(Q[r-1], Q[r], i)) r--; r++; Q[r] = i; } swap(new_dp, dp); } cout << *max_element(dp.begin(), dp.end()) << "\n"; }
#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...