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>
using namespace std;
#define INIT ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll
int t, n, m, k, a[100010], q, l, r;
int p[100010];
int dp[100010][2];
int32_t dir[100010][202];
bool upper(pair<pair<int, int>, pii> l1, pair<pair<int,int>, pii> l2){
return ( (l1.sc.ft*(l2.ft.ft)+l1.sc.sc)>(l2.sc.ft*(l2.ft.ft)+l2.sc.sc) );
}
int intersect(pair<pair<int, int>, pii> l1, pair<pair<int, int>, pii> l2){
int a1=l1.sc.ft, b1=l1.sc.sc, a2=l2.sc.ft, b2=l2.sc.sc;
if( ((b2-b1)%(a1-a2))==0 ){
return ( ((b2-b1)/(a1-a2)) );
}
else{
return ( ((b2-b1)/(a1-a2))+1 );
}
}
void convex_hull(int g){
vector<pair<pair<int, int>, pii> > st;
for(int j=n; j>=((g==1)?(0):(1)); j--){
pair<pair<int,int >, pii> ac={ {0ll, j}, {-p[j], dp[j][0]}};
while( (!st.empty()) && ((upper(ac, st.back())) ) ){
st.pop_back();
}
if(st.empty()){
st.pb(ac);
}
else{
if(ac.sc.ft==st.back().sc.ft){ac.ft.ft=st.back().ft.ft; st.pb(ac);}
else{
st.pb({{intersect(ac, st.back()), j}, ac.sc});}
}
}
int j=0;
for(int i=n; i>=1; i--){
while((j<(st.size()-1) ) && ( ( ((st[j+1].ft.ft<=(p[n]-p[i])) && (st[j].ft.ft<(p[n]-p[i])) ) || (st[j].ft.sc==i) )) ){
j++;
}
dp[i][1]=(p[n]-p[i])*st[j].sc.ft+st[j].sc.sc+p[i]*(p[n]-p[i]);
dir[i][g]=st[j].ft.sc;
}
for(int i=1; i<=n ;i++){
dp[i][0]=dp[i][1];
}
}
int32_t main(){
INIT
cin>>n>>k;
for(int i=1; i<=n; i++){
cin>>a[i];
p[i]=(p[i-1]+a[i]);
}
for(int g=1; g<=(k+1); g++){
convex_hull(g);
}
cout<<(dp[n][0] )<<"\n"<<flush;
int cnt=0, cr=n;
while(cnt<k){
cout<<dir[cr][k-cnt+1]<<" "; cr=dir[cr][k-cnt+1]; cnt++;
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'void convex_hull(long long int)':
sequence.cpp:58:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<std::pair<long long int, long long int>, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | while((j<(st.size()-1) ) && ( ( ((st[j+1].ft.ft<=(p[n]-p[i])) && (st[j].ft.ft<(p[n]-p[i])) ) || (st[j].ft.sc==i) )) ){
| ~^~~~~~~~~~~~~~
# | 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... |