이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ld long long
#define ll long long
const ld INF=2e9;
const ll MAXN=1e5+6;
const ll MAXK=202;
ll dp[MAXN][2],a[MAXN];
ll last1[208][208];
ll dp1[208][208];
int last[MAXN][MAXK];
struct line
{
ld a,b;
ll pos;
ld intersect(line const& other)
{
if(other.a==a)return -INF;
else return (ld)(other.b-b)/(a-other.a);
}
ld gety(ld x)
{
return (ld)a*x+b;
}
};
struct ConvexHull
{
deque<line>dq;
bool yoda(line l)
{
ld int1=l.intersect(dq.back());
ld int2=dq.back().intersect(dq[dq.size()-2]);
ld int3=l.intersect(dq[dq.size()-2]);
if(int1==-INF)
{
if(l.b>=dq.back().b)return 1;
else return 0;
}
if(int3>int2)return 1;
else return 0;
}
void add(line l)
{
while(dq.size()>1&&yoda(l))
{
dq.pop_back();
}
if(dq.empty())dq.push_back(l);
else if(!(l.a==dq.back().a&&l.b>dq.back().b))
{
dq.push_back(l);
}
}
/*pair<ld,ld> query(ld x)
{
ll l=0,r=dq.size()-1,mid;
while(l<r)
{
mid=(l+r)/2;
if(dq[mid].gety(x)<dq[mid+1].gety(x))
{
l=mid+1;
}
else r=mid;
}
//cout<<x<<" "<<dq[l].a<<" "<<dq[l].b<<endl;
return {dq[l].gety(x),dq[l].pos};
}*/
pair<ld,ld> query(ld x)
{
while(dq.size()>1&&dq[1].gety(x)>dq[0].gety(x))
{
dq.pop_front();
}
return {dq.front().gety(x),dq.front().pos};
}
}ch[2];
ll n,k;
ll pref[MAXN];
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
cout.tie(NULL);
cin>>n>>k;
for(ll i=1;i<=n;++i)
{
cin>>a[i];
pref[i]=pref[i-1]+a[i];
}
if(n<=200)
{
for(int i=0;i<=n;i++)
{
for(int j=0;j<=k;j++)
{
dp1[i][j]=-1;
}
}
for(int i=1;i<=n;i++)
{
dp1[i][0]=0;
}
for(int j=1;j<=k;j++)
{
for(int i=1;i<=n;i++)
{
for(int i1=1;i1<i;i1++)
{
if(dp1[i1][j-1]+pref[i1]*(pref[i]-pref[i1])>dp1[i][j])
{
dp1[i][j]=dp1[i1][j-1]+pref[i1]*(pref[i]-pref[i1]);
last1[i][j]=i1;
}
}
}
}
cout<<dp1[n][k]<<endl;
int u=n;
while(u!=0)
{
u=last1[u][k];
k--;
if(u==0)break;
cout<<u<<" ";
}
cout<<endl;
return 0;
}
ch[0].add({0,0,0});
for(int j=1;j<=k+1;++j)
{
ch[j%2].dq.clear();
ch[j%2].add({0,-INF,-1});
for(int i=1;i<=n;++i)
{
//tie(dp[i][j%2],last[i][j])=ch[1-j%2].query(-pref[i]);
pair<ld,ld> p=ch[1-j%2].query(-pref[i]);
dp[i][j%2]=p.first;
last[i][j]=p.second;
}
for(int i=1;i<=n;++i)
{
ch[j%2].add({(ld)-pref[i],(ld)dp[i][j%2]-pref[i]*pref[i],i});
/*for(auto xd:ch[j%2].dq)cout<<xd.a<<","<<xd.b<<" ";
cout<<endl;*/
}
}
cout<<dp[n][1-k%2]<<"\n";
if(dp[n][1-k%2]==0)
{
for(int j=1;j<=k;++j)
{
cout<<j<<" ";
}
cout<<"\n";
return 0;
}
vector<ll>v;
ll n1=n;
for(ll i=k;i>=1;--i)
{
v.push_back(last[n1][i+1]);
n1=last[n1][i+1];
}
for(auto xd:v)cout<<xd<<" ";
cout<<"\n";
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... |