제출 #107914

#제출 시각아이디문제언어결과실행 시간메모리
107914SamAnd수열 (APIO14_sequence)C++17
71 / 100
1053 ms132096 KiB
#include <bits/stdc++.h> using namespace std; const int N = 100005; const long long INF = 1000000009000000009; int n, m; int a[N]; long long p[N]; long long dp[N][202]; int pp[N][202]; vector<int> v[202]; vector<long double> x[202]; long double hat(long long b1, long long k1, long long b2, long long k2) { return 1.0 * (b2 - b1 + 0.0) / (k1 - k2 + 0.0); } void ave(int k, int j) { while (!v[k].empty() && p[v[k].back()] == p[j]) { v[k].pop_back(); if (!x[k].empty()) x[k].pop_back(); } while (v[k].size() >= 2) { long double x1 = hat(dp[v[k].back()][k] - p[v[k].back()] * p[v[k].back()], p[v[k].back()], dp[j][k] - p[j] * p[j], p[j]); if (x1 > x[k].back()) break; v[k].pop_back(); x[k].pop_back(); } if (v[k].empty()) { v[k].push_back(j); return; } long double x1 = hat(dp[v[k].back()][k] - p[v[k].back()] * p[v[k].back()], p[v[k].back()], dp[j][k] - p[j] * p[j], p[j]); x[k].push_back(x1); v[k].push_back(j); } int byn(int i, int k) { int l = 0, r = x[k - 1].size() - 1; while ((r - l) > 3) { int m = (l + r) / 2; if (p[i] >= x[k - 1][m]) l = m; else r = m; } for (int m = r; m >= l; --m) { if (p[i] >= x[k - 1][m]) return v[k - 1][m + 1]; } return v[k - 1][0]; } int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) p[i] = p[i - 1] + a[i]; for (int i = 1; i <= n; ++i) { for (int k = 1; k <= min(m, i - 1); ++k) { int j = byn(i, k); dp[i][k] = dp[j][k - 1] - p[j] * p[j] + p[j] * p[i]; pp[i][k] = j; } for (int k = 0; k <= min(m, i - 1); ++k) { ave(k, i); } } cout << dp[n][m] << endl; int i = n; for (int k = m; k > 0; --k) { cout << pp[i][k] << ' '; i = pp[i][k]; } cout << endl; 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...