제출 #337424

#제출 시각아이디문제언어결과실행 시간메모리
337424ogibogi2004수열 (APIO14_sequence)C++14
0 / 100
85 ms131076 KiB
#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
const ld INF=2e9;
const int MAXN=1e5+6;
const int MAXK=202;
ll dp[MAXN][MAXK],a[MAXN];
struct line
{
	ld a,b;
	ld intersect(line const& other)
	{
		if(a==other.a)return -INF;
		return (ld)(other.b-b)/(a-other.a);
	}
	ld gety(ld x)
	{
		return a*x+b;
	}
};
struct ConvexHull
{
	deque<pair<line,double> >dq;
	void add(line l)
	{
		while(!dq.empty()&&l.intersect(dq.back().first)<=dq.back().second)
		{
			dq.pop_back();
		}
		if(dq.empty())dq.push_back({l,-INF});
		else dq.push_back({l,l.intersect(dq.back().first)});
	}
	ld query(ld x)
	{
		while(dq.size()>1&&dq[1].second<=x)dq.pop_front();
		//cout<<dq.front().first.a<<" "<<dq.front().first.b<<endl;
		return dq.front().first.gety(x);
	}
}ch[MAXK];
int n,k;
int pref[MAXN];
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		pref[i]=pref[i-1]+a[i];
	}
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=k+1;j++)dp[i][j]=-INF;
	}
	for(int i=1;i<=k+1;i++)ch[i].add({0,-INF});
	ch[0].add({0,0});
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=k+1;j++)
		{
			dp[i][j]=ch[j-1].query(pref[i]);
			//cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
		}
		for(int j=1;j<=k+1;j++)
		{
			ch[j].add({(ld)pref[i],(ld)dp[i][j]-pref[i]*pref[i]});
		}
	}
	cout<<dp[n][k+1]<<endl;
	vector<int>v;
	int l=n;
	for(int i=k;i>=1;i--)
	{
		for(int l1=l-1;l1>=0;l1--)
		{
			if(dp[l1][i]+(pref[l]-pref[l1])*pref[l1]==dp[l][i+1])
			{
				v.push_back(l1);
				l=l1;
				break;
			}
		}
	}
	for(auto xd:v)cout<<xd<<" ";
	cout<<endl;
return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...