제출 #23336

#제출 시각아이디문제언어결과실행 시간메모리
23336Nurlykhan수열 (APIO14_sequence)C++14
100 / 100
1316 ms83984 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) 1e5 + 7; const int M = (int) 2e6 + 7; const int K = 202; const ll LINF = (ll) 1e16; const int INF = (int) 1e9 + 7; const double EPS = (double) 1e-9; ll dp[N][2], a[N]; int p[N][K]; int n, k; ll sum(int l, int r) { if (l == 0) { return a[r]; } return a[r] - a[l - 1]; } ld inter(int i, int x, int y) { if (a[y] == a[x]) return INF; return (dp[y][i % 2] - dp[x][i % 2]) * 1.0 / (a[y] - a[x]); } int st[N]; int l, r; ll f(int i, int j, ll x) { return x * 1ll * a[i] + dp[i][j % 2]; } int get(int j, int x) { int id = l; while (id + 1 <= r && f(st[id + 1], j, x) >= f(st[id], j, x)) id++; return st[id]; } ll dp1[202][202]; void solve() { for (int i = 1; i <= n; i++) { dp1[i][1] = sum(1, i) * sum(i + 1, n); } for (int i = 2; i <= k; i++) { for (int j = i; j <= n; j++) { for (int t = 1; t < j; t++) { if (dp1[j][i] < dp1[t][i-1] + sum(j + 1, n) * sum(t + 1, j)) { dp1[j][i] = dp1[t][i-1] + sum(j + 1, n) * sum(t + 1, j); p[j][i] = t; } } } } int j = 1; for (int i = 1; i <= n; i++) { if (dp1[j][k] < dp1[i][k]) j = i; } cout << dp1[j][k] << endl; vector<int> ans; while (j > 0 && k > 0) { ans.pb(j); j = p[j][k]; k--; } reverse(all(ans)); for (auto it : ans) cout << it << " "; exit(0); } 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]; } if (n <= 200) { //assert(0); solve(); } for (int i = 1; i <= n; i++) { dp[i][1] = sum(1, i) * sum(i + 1, n); } for (int i = 2; i <= k; i++) { l = 1, r = 0; for (int j = 1; j <= n; j++) { while (l + 1 <= r && inter(i - 1, st[r], j) - inter(i - 1, st[r - 1], st[r]) > -EPS) { --r; } while (l + 1 <= r && f(st[l], i - 1, -sum(j + 1, n)) <= f(st[l + 1], i - 1, -sum(j + 1, n))) { ++l; } /* y[t] = (a[j] - a[t]) * (a[n] - a[j]) + dp[t][i - 1] a[j]*a[n]-a[j]*a[j]-a[t]*a[n]+a[t]*a[j] a[t]*(-a[n]+a[j])+dp[t][i-1]+a[j]*(a[n]-a[j]) k=a[j]-a[t] */ dp[j][i % 2] = 0; //if (l <= r) { for (int it = max(1, l - 2); it <= min(l + 2, r); it++) { int t = st[it]; if (dp[j][i % 2] < dp[t][1 - i % 2] + sum(j + 1, n) * sum(t + 1, j)) { dp[j][i % 2] = dp[t][1 - i % 2] + sum(j + 1, n) * sum(t + 1, j); p[j][i] = t; } } //} st[++r] = j; } } int j = 1; for (int i = 1; i <= n; i++) { if (dp[j][k % 2] < dp[i][k % 2]) j = i; } cout << dp[j][k % 2] << endl; vector<int> ans; while (j > 0 && k > 0) { ans.pb(j); j = p[j][k]; k--; } reverse(all(ans)); for (auto it : ans) cout << it << " "; 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...