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>
#define F first
#define S second
#define pb push_back
//#define endl '\n'
#define ll long long
#define pll pair<ll,ll>
#define pi pair<int,int>
using namespace std;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAXN = 1e9+1;
const int N = 1005;
struct Line{
ll m, b, id;
ll eval(ll x) {
return m*x + b;
}
};
struct node{
node *l, *r;
Line ln;
node(node* _l, node* _r, Line _ln):
l(_l), r(_r), ln(_ln) {};
};
typedef node* pnode;
Line get(pnode rt) {
return (!rt ? Line({0, -INF, -1}) : rt -> ln);
}
void add(pnode &rt, Line ln, int l=0, int r=MAXN, int v = 1) {
if (!rt) rt = new node(NULL, NULL, Line({0, -INF, -1}));
int m = (l + r) >> 1;
bool left = ln.eval(l) > get(rt).eval(l);
bool mid = ln.eval(m) > get(rt).eval(m);
if (mid) swap(ln, rt -> ln);
if (r - l == 1) return;
if (left != mid) add(rt -> l, ln, l, m, v << 1);
else add(rt -> r, ln, m, r, v << 1 | 1);
}
pll get(pnode &rt, ll x, int l=0, int r=MAXN, int v=1) {
if (rt == NULL) rt = new node(NULL, NULL, Line({0, -INF, -1}));
int m = (l + r) >> 1;
pll now = {get(rt).eval(x), get(rt).id};
if (r - l == 1) return now;
if (x < m) return max(now, get(rt -> l, x, l, m, v << 1));
else return max(now, get(rt -> r, x, m, r, v << 1 | 1));
}
ll dp[201][N];
int opt[201][N], ps[N];
pnode rt;
int main() {
ios_base :: sync_with_stdio(0);
cin.tie(0);
int n, K;
cin >> n >> K; K++;
for (int i = 1, x; i <= n; i++) {
cin >> x;
ps[i] = ps[i - 1] + x;
dp[0][i] = -INF;
}
dp[0][0] = 0;
pll ans;
for (int k = 1; k <= K; k++) {
rt = NULL;
if (k == 1) add(rt, Line({0, 0, 0}));
for (int i = 1; i <= n; i++) {
//cout << "getting ans for " << ps[i] << endl;
ans = get(rt, ps[i]);
dp[k][i] = ans.F;
opt[k][i] = ans.S;
//cout << k << " " << i << " " << opt[k][i] << endl;
add(rt, Line({ps[i], dp[k - 1][i] - 1ll*ps[i]*ps[i], i}));
}
}
cout << dp[K][n] << endl;
vector<int> res;
int id = n, ptr = K;
while(res.size() < K - 1) {
res.pb(opt[ptr][id]);
id = opt[ptr--][id];
}
reverse(res.begin(), res.end());
for (int i : res) cout << i << " ";
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:85:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
85 | while(res.size() < K - 1) {
| ~~~~~~~~~~~^~~~~~~
# | 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... |