Submission #94855

#TimeUsernameProblemLanguageResultExecution timeMemory
94855OrigimsSplit the sequence (APIO14_sequence)C++14
71 / 100
128 ms132096 KiB
// In The Name Of God #include <bits/stdc++.h> #include<ext/pb_ds/assoc_container.hpp> #include<ext/pb_ds/tree_policy.hpp> #define sqr(A) ((A) * (A)) #define X first #define Y second #define MP make_pair #define bsz __builtin_popcount #define all(A) A.begin(), A.end() using namespace std; using namespace __gnu_pbds; template < typename T > using ordered_set = tree < T , null_type , less < T > , rb_tree_tag , tree_order_statistics_node_update >; typedef long long ll; typedef long double ld; typedef pair<ll, ll> pii; typedef complex<ll> point; const int MOD = 1e9 + 7; const int MOD2 = 1e9 + 9; const int PR = 727; const int INF = INT_MAX; const ll LINF = LLONG_MAX; struct convexHull { vector<point> lines; vector<int> idx; int pos = 0; ll F(point p, ll x) { return p.real() * x + p.imag(); } ld I(point p1, point p2) { return (ld)(p2.imag() - p1.imag()) / (ld)(p1.real() - p2.real()); } void add(point p, int id) { if (lines.size() && p.real() == lines.back().real()) return ; while (lines.size()) { point a = lines[lines.size() - 1]; point b = lines[lines.size() - 2]; if (I(p, b) <= I(b, a)) { lines.pop_back(); idx.pop_back(); } else break; } lines.push_back(p); idx.push_back(id); } pii get(ll x) { while (pos + 1 < lines.size() && I(lines[pos], lines[pos + 1]) <= (ld)x) pos++; return MP(F(lines[pos], x), idx[pos]); } void clear() { lines.clear(); idx.clear(); pos = 0; } }; const int N = 1e5 + 20; const int K = 2e2 + 20; ll a[N], dp[N], p[N], par[N][K], n, k; vector<int> ans; convexHull CHT; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; k++; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { p[i] = a[i] + p[i - 1]; dp[i] = sqr(p[i]); } for (int i = 2; i <= k; i++) { CHT.add({p[i - 1] * -2, dp[i - 1] + sqr(p[i - 1])}, i - 1); for (int j = i; j <= n; j++) { pii tmp = CHT.get(p[j]); ll D = dp[j]; dp[j] = tmp.X + sqr(p[j]); par[j][i] = tmp.Y; CHT.add({p[j] * -2, D + sqr(p[j])}, j); } CHT.clear(); } int v = par[n][k]; for (int i = k - 1; i; i--) { ans.push_back(v); v = par[v][i]; } // cout << sqr(p[n]) << ' ' << dp[n] << endl; cout << (sqr(p[n]) - dp[n]) / 2 << endl; for (int i = ans.size() - 1; i >= 0; i--) cout << ans[i] << ' '; cout << endl; }

Compilation message (stderr)

sequence.cpp: In member function 'pii convexHull::get(ll)':
sequence.cpp:56:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while (pos + 1 < lines.size() && I(lines[pos], lines[pos + 1]) <= (ld)x) pos++;
          ~~~~~~~~^~~~~~~~~~~~~~
#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...