제출 #556682

#제출 시각아이디문제언어결과실행 시간메모리
556682timreizin수열 (APIO14_sequence)C++17
100 / 100
746 ms86876 KiB
#pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt") #include <iostream> #include <vector> #include <algorithm> #include <deque> #include <set> #include <string> #include <numeric> #include <cmath> #include <cassert> #include <array> #include <numeric> using namespace std; using ll = long long; const ll INF = 1e18; struct Line { ll k, b; int who; Line(ll k = 0, ll b = 0, int who = 0) : k(k), b(b), who(who) {} ll operator()(ll x) { return k * x + b; } }; bool check(Line a, Line b, Line c) { if (b.k == c.k) return b.b >= c.b; return (__int128)(a.b - c.b) * (b.k - a.k) <= (__int128)(a.b - b.b) * (c.k - a.k); } int main() { cin.tie(0)->sync_with_stdio(0); int n, k; cin >> n >> k; ++k; vector<ll> a(n); for (ll &i : a) cin >> i; array<vector<ll>, 2> dp; dp[0] = dp[1] = vector<ll>(n + 1, INF); dp[0][0] = 0; ll res = accumulate(a.begin(), a.end(), 0ll); res = res * res; partial_sum(a.begin(), a.end(), a.begin()); a.insert(a.begin(), 0); vector<vector<int>> parent(n + 1, vector<int>(k, -1)); for (int i = 0; i < k; ++i) { deque<Line> dq; dq.push_back({-2 * a[i], dp[0][i] + a[i] * a[i], i}); for (int j = i + 1; j <= n; ++j) { while (dq.size() >= 2 && dq[0](a[j]) > dq[1](a[j])) dq.pop_front(); dp[1][j] = dq.front()(a[j]) + a[j] * a[j]; parent[j][i] = dq.front().who; Line add{-2 * a[j], dp[0][j] + a[j] * a[j], j}; while (dq.size() >= 2 && check(dq[(int)dq.size() - 2], dq.back(), add)) dq.pop_back(); if (dq.back().k != add.k) dq.push_back(add); } dp[0] = dp[1]; dp[1] = vector<ll>(n + 1, INF); } vector<int> path(k - 1); for (int i = k - 1, v = n; i > 0; v = parent[v][i], --i) path[k - i - 1] = parent[v][i]; cout << (res - dp[0].back()) / 2 << '\n'; reverse(path.begin(), path.end()); for (int i : path) cout << i << ' '; 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...