# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1106721 | luvna | Split the sequence (APIO14_sequence) | C++17 | 1252 ms | 131072 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 MASK(i) (1 << (i))
#define pub push_back
#define all(v) v.begin(), v.end()
#define compact(v) v.erase(unique(all(v)), end(v))
#define pii pair<int,int>
#define fi first
#define se second
#define endl "\n"
#define sz(v) (int)(v).size()
#define int long long
using namespace std;
template<class T> bool minimize(T& a, T b){if(a > b) return a = b, true;return false;}
template<class T> bool maximize(T& a, T b){if(a < b) return a = b, true;return false;}
typedef long long ll;
typedef long double ld;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r){return uniform_int_distribution<ll>(l, r)(rng);}
const int N = 1e5 + 15;
int n, k;
int pref[N];
ll dp[2][N];
bool past = false, succ = true;
struct line {
ll m, c, id;
ll compute(ll x) {return m * x + c;}
};
bool useless(line d1, line d2, line d3) {
return (d1.c - d2.c) * (d3.m - d2.m) >= (d2.c - d3.c) * (d2.m - d1.m);
}
struct CHT {
vector<line> lines;
void reload() {
while(!lines.empty()) lines.pop_back(); lines.push_back({0, 0, 0});
}
void add(line d) {
while(lines.size() > 1 && useless(d, lines.back(), lines[lines.size() - 2]))
lines.pop_back();
lines.push_back(d);
}
pii query(ll x) {
int l = 0, r = lines.size() - 1;
while(l < r) {
int m = (l + r) >> 1;
if(lines[m].compute(x) <= lines[m + 1].compute(x)) l = m + 1;
else r = m;
}
return {lines[l].compute(x), lines[l].id};
}
};
CHT solver;
int trace[205][N];
void solve(){
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> pref[i], pref[i] += pref[i-1];
for(int j = 1; j <= k; j++){
solver.reload(); swap(succ, past);
for(int i = 1; i <= n; i++){
pii e = solver.query(pref[n] - pref[i]);
trace[j][i] = e.se;
dp[succ][i] = e.fi + 1LL*pref[i]*(pref[n] - pref[i]);
solver.add({-pref[i], dp[past][i], i});
}
}
int pos = -1;
ll ans = -1;
for(int i = 1; i <= n; i++){
if(maximize(ans, dp[succ][i])) pos = i;
}
vector<int> res;
for(int i = k; i >= 1; i--) res.push_back(pos), pos = trace[i][pos];
cout << ans << endl;
for(int i = k-1; i >= 0; i--) cout << res[i] << " ";
}
signed main(){
ios_base::sync_with_stdio(NULL);
cin.tie(0); cout.tie(0);
#define task "task"
if(fopen(task".INP", "r")){
freopen(task".INP", "r", stdin);
freopen(task".OUT", "w", stdout);
}
int t; t = 1; //cin >> t;
while(t--) solve();
}
Compilation message (stderr)
# | 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... |