# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
611152 | Do_you_copy | Split the sequence (APIO14_sequence) | C++17 | 0 ms | 0 KiB |
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 taskname "test"
#define fi first
#define se second
#define pb push_back
#define faster ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
using ll = long long;
using ull = unsigned ll;
using ld = long double;
using pii = pair <int, int>;
using pil = pair <int, ll>;
using pli = pair <ll, int>;
using pll = pair <ll, ll>;
mt19937 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll min(const ll &a, const ll &b){
return (a < b) ? a : b;
}
ll max(const ll &a, const ll &b){
return (a > b) ? a : b;
}
//const ll Mod = 1000000007;
//const ll Mod2 = 999999999989;
//only use when required
const int maxN = 1e5 + 1;
int n, k;
int a[maxN];
int dp[maxN][201];
int trace[maxN][201];
int pre[maxN];
struct TLine{
int fi, se, idx;
};
inline ld intersect(const TLine &i, const TLine &j){
return ld(i.se - j.se) / ld(j.fi - i.fi);
}
inline cal(const TLine &i, int x){
return i.fi * x + i.se;
}
deque <TLine> S[201];
void Init(){
cin >> n >> k;
++k;
for (int i = 1; i <= n; ++i){
cin >> a[i];
pre[i] = pre[i - 1] + a[i];
}
S[0].pb({0, 0, 0});
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= k; ++j){
if (S[j - 1].empty()) break;
int x = pre[n] - pre[i];
while (S[j - 1].size() > 1 && cal(S[j - 1][0], x) <= cal(S[j - 1][1], x)) S[j - 1].pop_front();
TLine f = S[j - 1][0];
dp[i][j] = cal(f, x) + pre[i] * x;
trace[i][j] = f.idx;
TLine newline = {-pre[i], dp[i][j], i};
while (S[j].size() > 1 && intersect(S[j].back(), S[j][S[j].size() - 2]) <= intersect(S[j].back(), newline)) S[j].pop_back();
S[j].pb(newline);
}
}
cout << dp[n][k] << "\n";
int idx = n;
vector <int> ans;
for (int i = k; i && trace[idx][i]; --i){
idx = trace[idx][i];
ans.pb(idx);
}
reverse(ans.begin(), ans.end());
for (int i: ans) cout << i << " ";
}
int main(){
if (fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
//freopen(taskname".out", "w", stdout);
}
faster;
ll tt = 1;
//cin >> tt;
while (tt--){
Init();
}
}