This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template <class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) cerr << *i << " "; cerr << "]\n";}
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
typedef long double ld;
const int mx = 1e5+5;
struct line {
int idx;
ll m, b;
line(ll mm, ll bb, int ii) : idx(ii), m(mm), b(bb) {}
ll eval(ll x) {return m*x+b;}
ld isectx(line l) {return (ld)(l.b-b)/(m-l.m);}
};
ll a[mx];
int prv[mx][205];
deque<line> stuff[205];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n,k;
cin >> n >> k;
for (int q = 1; q <= n; q++) cin >> a[q];
stuff[0].push_back(line(0,0,0));
ll cur = 0, ans = 0;
for (int q = 1; q <= n; q++) {
cur += a[q];
for (int w = min(q,k+1); w >= 1; w--) {
while (SZ(stuff[w-1]) >= 2 && stuff[w-1][0].eval(cur) >= stuff[w-1][1].eval(cur)) stuff[w-1].pop_front();
ll val = stuff[w-1][0].eval(cur)+cur*cur;
if (q == n && w == k+1) ans = val;
prv[q][w] = stuff[w-1][0].idx;
if (w > 1) assert(prv[q][w] != 0);
line ins = line(cur*-2,val+cur*cur,q);
while (SZ(stuff[w])) {
if (stuff[w].back().m == ins.m) {
if (ins.b < stuff[w].back().b) stuff[w].pop_back();
else break;
} else if (SZ(stuff[w]) >= 2) {
if (ins.isectx(stuff[w].back()) <= ins.isectx(stuff[w][SZ(stuff[w])-2])) stuff[w].pop_back();
else break;
} else {
break;
}
}
if (SZ(stuff[w]) == 0 || stuff[w].back().m != ins.m) stuff[w].push_back(ins);
}
}
cout << (cur*cur-ans)/2 << "\n";
int ri = n, cuts = k+1;
while (cuts > 1) {
cout << prv[ri][cuts] << " ";
ri = prv[ri][cuts];
cuts--;
}
cout << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |