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> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#define pb push_back
#define For(i,s,e) for (ll i=(s); i<(e); i++)
#define Debug_array(a,n) for (ll i=(0); i<(n); i++) cout<<a[i]<<" "
#define Foe(i,s,e) for (ll i=(s); i<=(e); i++)
#define Fod(i,s,e) for (ll i=(s)-1; i>=(e); i--)
#define pii pair<ll,ll>
#define fi first
#define se second
#define endl "\n"
#define mp make_pair
#define big_prime 15486277
#define bigger_prime 179424697
#define biggest_prime 32416188691
//#define random_shuffle(indices.begin(), indices.end());
//std::random_device rd;
//std::mt19937 g(rd());
//std::shuffle(v.begin(), v.end(), g);
using namespace __gnu_pbds;
using namespace std;
template <class T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T> ostream& operator << (ostream &os, const set<T> &v) { for (T i : v) os << i << ' '; return os; }
template <class T, class S> ostream& operator << (ostream &os, const pair<T, S> &v) { os << v.first << ' ' << v.second; return os; }
// template <class T, class S> ostream& operator << (ostream &os, const unordered_map<T, S> &v) { for (auto i : v) os << '(' << i.first << "=>" << i.second << ')' << ' '; return os; }
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << endl; }
template <class Arg1, class... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* sep = strchr(names + 1, ',');
cerr.write(names, sep - names) << " : " << arg1 << " ";
__f(sep + 1, args...);
}
#else
#define trace(...) 0
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE
typedef long long ll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define Mod 1000000007
#define MAX 100001
ll dp[MAX][201],Next[MAX][201];
//CHT
struct Line {
mutable ll k, m, p;
bool operator<(const Line& o) const { return k < o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) { x->p = inf; return false; }
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
//NOTE: Here 'k' represents slope of the line and 'm' represents the y-intercept
void add(ll k, ll m) {
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
//Querying for the max value of 'x' in all linear functions
pair<pii,ll> query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return {{l.k,l.m} , l.k * x + l.m};
}
};
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
memset(Next,-1,sizeof(Next));
int n,K;cin>>n>>K;
ll a[n],p[n];
For(i,0,n) {
cin>>a[i];
p[i] = a[i]+(i>0?p[i-1]:0);
}
LineContainer L;
vector<pii> to_add(n);
Fod(i,n-1,0) {
to_add[i] = {p[i]-p[n-1],p[i]*(p[n-1]-p[i])};
}
Foe(k,1,K){
L.clear();
map<pii,ll> M;
Fod(i,n-1,0){
L.add(to_add[i].fi,to_add[i].se);
M[to_add[i]]=i;
ll P = (i>0 ? p[i-1] : 0);
pair<pii,ll> ans = L.query(P);
dp[i][k] = ans.se;
Next[i][k] = M[ans.fi];
to_add[i]={p[i]-p[n-1],dp[i+1][k]+p[i]*(p[n-1]-p[i])};
}
}
cout<<dp[0][K]<<endl;
vector<ll> final;
ll start = 0;
while(K>0){
start = Next[start][K]+1;
final.pb(start);
K--;
}
for(ll x:final){
cout<<x<<" ";
}
cout<<endl;
}
# | 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... |