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"
using namespace std;
#define forR(i, a) for(int i = 0; (i) < (a); ++(i))
#define REP(i, a, b) for(int i = (a); (i) < (b); ++i)
#define all(a) a.begin(), a.end()
#define boost() cin.sync_with_stdio(0); cin.tie(0)
#define printArr(arr) for(int asdfg : arr) cout << asdfg << ' '; cout << '\n'
#define open(x) freopen(((string) x + ".in").c_str(), "r", stdin); freopen(((string) x + ".out").c_str(), "w", stdout);
typedef long long ll;
typedef long double ld;
struct pii{ll a, b;};
struct tii{ll a, b, c;};
bool operator <(pii a, pii b){ return a.a < b.a || a.a == b.a && a.b < b.b;}
struct line{int m; ll b; int i;
ll operator ()(ll x){return m*x + b;}
};
ll intx(line& a, line& b){
return (b.b-a.b)/(a.m-b.m);
}
const int MN = 1e5 + 10, MK = 210;
int s[MN];
line pre[MN];
int rem[MN][MK];
signed main(){
int n, k; cin >> n >> k;
forR(i, n) cin >> s[i];
REP(i, 1, n) s[i] += s[i-1];
forR(g, k + 1){
deque<line> lin = {{0, 0, -1}};
forR(i, n){
while(lin.size() > 1 && lin[1](s[i]) >= lin[0](s[i])) lin.pop_front();
ll ans = lin[0](s[i]);
rem[i][g] = lin[0].i;
if(g == k && i == n - 1){
cout << ans << '\n';
for(int cur = i, j = k; j >= 1; cur=rem[cur][j--]) {
cout << rem[cur][j] + 1 << " \n"[j == 1];
}
break;
}
int si = lin.size();
if(lin[si-1].m == pre[i].m){
if(lin[si-1].b <= pre[i].b) lin.pop_back();
else {
pre[i] = {s[i], (ll) -s[i] * s[i] + ans};
continue;
}
}
si = lin.size();
for(; si > 1 && intx(lin[si-1], pre[i]) <= intx(lin[si-2], lin[si-1]); si=lin.size()) lin.pop_back();
lin.push_back(pre[i]);
pre[i] = {s[i], (ll) -s[i] * s[i] + ans, i};
}
}
}
Compilation message (stderr)
sequence.cpp: In function 'bool operator<(pii, pii)':
sequence.cpp:14:63: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
14 | bool operator <(pii a, pii b){ return a.a < b.a || a.a == b.a && a.b < b.b;}
| ~~~~~~~~~~~^~~~~~~~~~~~
# | 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... |