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
int Next[MAX][201];
ll dp[MAX][2];
//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;
//
// //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
// ll query(ll x) {
// assert(!empty());
// auto l = *lower_bound(x);
// return l.k * x + l.m;
// }
// };
struct Line
{
ll m,c;
Line(ll m,ll c) : m(m) , c(c) {}
};
ll DIV(ll a, ll b) { // floored DIVision
return a / b - ((a ^ b) < 0 && a % b); }
ll get_intersection(Line one,Line two){
ll num = (two.c-one.c);
ll den = (one.m-two.m);
return DIV(num,den);
}
bool can_remove(Line x,Line y,Line z) {
if(get_intersection(x,z) > get_intersection(y,z)){
return 1;
}else return 0;
}
void add(deque<pair<Line,int> >& s,ll m,ll c,int idx){
if(s.size()==0){
s.push_back({Line(m,c),idx});
return;
}
if(s.back().fi.m==m){
if(s.back().fi.c>c) return;
else{
s.pop_back();
s.push_back({Line(m,c),idx});
return;
}
}
if(s.size()==1){
s.push_back({Line(m,c),idx});
return;
}
Line x(m,c);
while(s.size()>1){
int y_idx = s.back().se;
Line y = s.back().fi;s.pop_back();
Line z = s.back().fi;
if(can_remove(x,y,z)){
continue;
}else{
s.push_back({y,y_idx});
break;
}
}
s.push_back({x,idx});
return;
}
pair<ll,int> query(deque<pair<Line,int> >& s,ll pt){
// ll maxi = 0;
// for(Line l:s){
// maxi = max(maxi,l.m*pt+l.c);
// }
// return maxi;
assert(s.size()!=0);
while(s.size()>1){
int x_idx = s.front().se;
Line x = s.front().fi;s.pop_front();
Line y = s.front().fi;
if(x.m*pt+x.c > y.m*pt+y.c){
s.push_front({x,x_idx});
break;
}else{
continue;
}
}
// ll eval = s.front().fi.m*m+s.fi.front
// return mp(0,0);
return {s.front().fi.m*pt+s.front().fi.c,s.front().se};
}
signed 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);
}
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){
deque<pair<Line,int> > s;
Fod(i,n-1,0){
add(s,to_add[i].fi,to_add[i].se,i);
ll P = (i>0 ? p[i-1] : 0);
pair<ll,int> ans = query(s,P);
dp[i][k%2] = ans.fi;
Next[i][k] = ans.se;
to_add[i]={p[i]-p[n-1],dp[i+1][k%2]+p[i]*(p[n-1]-p[i])};
}
}
cout<<dp[0][K%2]<<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... |