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>
#include <ext/pb_ds/detail/standard_policies.hpp>*/
#define pb push_back
#define mp make_pair
#define sz(s) ((int)(s.size()))
#define all(s) s.begin(), s.end()
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define onlycin ios_base::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
/*typedef tree<
pair < int, int >,
null_type,
less< pair < int, int > >,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/
// find_by_order() order_of_key()
const int MAXN = (int)1e5+228;
const char nxtl = '\n';
const int mod = (int)1e9+7;
const double eps = (double)1e-7;
template<typename T> inline bool updmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<typename T> inline bool updmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
#define ld long double
struct line {
ll slope, delta;
int id;
line(ll _slope = 0, ll _delta = 0, int _id = 0) {
slope = _slope, delta = _delta, id = _id;
}
inline ld intersection(const line &X) const {
return (delta - X.delta - 0.0) / (X.slope - slope - 0.0);
}
inline ll get(const ll &x) {
return slope*x + delta;
}
};
vector < line > have;
int ptr = 0;
inline void addLine(const line &X) {
while(sz(have) > 1) {
ld int1 = have.back().intersection(have[sz(have)-2]);
if(X.slope == have.back().slope) {
if(X.delta >= have.back().delta) {
have.pop_back(); continue;
}
assert(0);
return;
}
ld int2 = X.intersection(have.back());
if(int2 <= int1) ptr--, have.pop_back();
else break;
}
updmax(ptr, 0);
have.pb(X);
// updmin(ptr, sz(have)-1);
}
inline ll Find(ll x) {
while(ptr+1 < sz(have) && have[ptr].get(x) < have[ptr+1].get(x)) ptr++;
return have[ptr].get(x);
}
int n, k, a[MAXN], come[202][MAXN];
ll pref[MAXN], dp[2][MAXN];
int main() {
#ifdef accepted
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
onlycin;
cin >> n >> k;
rep(i, 1, n) cin >> a[i];
rep(i, 1, n) pref[i] = pref[i-1]+a[i];
rep(i, 1, n) dp[1][i] = 0;
memset(come, 255, sizeof come);
rep(ii, 2, k+1) {
int i = ii%2;
rep(j, 1, n) dp[i][j] = -(ll)2e18;
ptr = 0;
have.clear();
rep(j1, 1, ii-1) {
addLine(line(pref[j1], dp[i^1][j1]-pref[j1]*pref[j1], j1));
}
rep(j, ii, n) {
dp[i][j] = Find(pref[j]);
come[ii][j] = have[ptr].id;
addLine(line(pref[j], dp[i^1][j]-pref[j]*pref[j], j));
/* rep(j1, 1, j-1) {
if(updmax(dp[i][j], dp[i^1][j1] + pref[j]*pref[j1]-pref[j1]*pref[j1])) come[ii][j] = j1;
}*/
}
}
k++;
cout << dp[k&1][n] << nxtl;
vector < int > res;
int i = n;
while(come[k][i] > 0) {
res.pb(come[k][i]);
i = come[k][i];
k--;
}
sort(all(res));
for(auto &to : res) cout << to << ' ';
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... |