This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
7 3
4 1 3 4 0 2 3
*/
#include<bits/stdc++.h>
using namespace std;
const int N=1e5 + 1;
const int mod=1e9 + 7;
int p[N][201];
#define int long long
const int inf=1e18;
#define pii pair<int, int>
#define f first
#define s second
#define mp make_pair
#define FOR(i, n) for(int i=1;i<=n;i++)
#define TRACE(x) cerr << #x << " = " << x << endl
//Trace prints the name of the variable and the value.
int dp[N][2], arr[N], n, k, pref[N];
deque<int> st;int ind;
void func(bool a)
{
if(a) return;
while(true) {}
}
pii inter(int a, int b)
{
return mp((dp[a][ind] - dp[b][ind]), (pref[a] - pref[b]));
}
bool comp(pii a, pii b)
{
return a.f*b.s<=a.s*b.f;
}
void remove()
{
while(st.size()>=3)
{
int i1=st.front(), i2, i3;st.pop_front();i2=st.front();st.pop_front();i3=st.front();
if(comp(inter(i1, i3), inter(i2, i3))) st.push_front(i1);
else {st.push_front(i2);st.push_front(i1);return;}
}
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>k;pref[0]=0;
if(n==1)
{
cout<<0;return 0;
}
for(int i=1;i<=n;i++) {cin>>arr[i];pref[i]=arr[i] + pref[i-1];}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=1;j++) dp[i][j]=0;
}
for(int i=1;i<=n;i++)
{
dp[i][0]=pref[i]*(pref[n]-pref[i]);
}
for(int j=2;j<=k;j++)
{
while(!st.empty()) st.pop_front();
st.push_front(j-1);ind=0;
for(int i=j;i<=n;i++)
{
while(st.size()>1)
{
int a=st.front();st.pop_front();int b=st.front();
if(dp[a][ind] - pref[a]*(pref[n]-pref[i])>dp[b][ind]-pref[b]*(pref[n]-pref[i]))
{st.push_front(a);break;}
}
int opt=st.front();
func(st.size()<=n);
// assert(opt>=1);assert(opt<i);
dp[i][1]=dp[opt][0] -pref[opt]*(pref[n]-pref[i]);
dp[i][1]+=(pref[i])*(pref[n]-pref[i]);p[i][j]=opt;
st.push_back(i);remove();
}
for(int i=1;i<=n;i++) dp[i][0]=dp[i][1];
}
int mi, mval=-inf;
for(int j=1;j<=n;j++)
{
if(mval<dp[j][1])
{
mi=j;mval=dp[j][1];
}
}
cout<<mval<<endl;
stack<int> sti;sti.push(mi);
while(sti.size()<k)
{
int cur=p[sti.top()][k-sti.size()+1];sti.push(cur);
}
while(!sti.empty()) {cout<<sti.top()<<" ";sti.pop();}
}
Compilation message (stderr)
sequence.cpp: In function 'int main()':
sequence.cpp:74:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
func(st.size()<=n);
~~~~~~~~~^~~
sequence.cpp:92:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(sti.size()<k)
~~~~~~~~~~^~
# | 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... |