Submission #337429

# Submission time Handle Problem Language Result Execution time Memory
337429 2020-12-20T18:26:40 Z ogibogi2004 Split the sequence (APIO14_sequence) C++14
0 / 100
84 ms 89964 KB
#include<bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
const ld INF=2e9;
const ll MAXN=1e5+6;
const ll MAXK=202;
ll dp[MAXN][2],a[MAXN];
int last[MAXN][MAXK];
struct line
{
	ld a,b;
	ll pos;
	ld llersect(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.llersect(dq.back().first)<=dq.back().second)
		{
			dq.pop_back();
		}
		if(dq.empty())dq.push_back({l,-INF});
		else dq.push_back({l,l.llersect(dq.back().first)});
	}
	pair<ld,int> 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),dq.front().first.pos};
	}
}ch[2];
ll n,k;
ll pref[MAXN];
int main()
{
	cin>>n>>k;
	for(ll i=1;i<=n;i++)
	{
		cin>>a[i];
		pref[i]=pref[i-1]+a[i];
	}
	/*for(ll i=0;i<=n;i++)
	{
		for(ll j=0;j<=k+1;j++)dp[i][j]=-INF;
	}*/
	//for(ll i=1;i<=k+1;i++)ch[i].add({0,-INF});
	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]);
			//cout<<i<<" "<<j<<" "<<last[i][j]<<endl;
		}
		for(int i=1;i<=n;i++)
		{
			ch[j%2].add({(ld)pref[i],(ld)dp[i][j%2]-pref[i]*pref[i],i});
		}
	}
	cout<<dp[n][1-k%2]<<endl;
	vector<ll>v;
	ll n1=n;
	for(ll i=k;i>=1;i--)
	{
		v.push_back(last[n1][i]);
		//cout<<n1<<" "<<i+1<<endl;
		n1=last[n1][i];
	}
	/*
	for(ll i=k;i>=1;i--)
	{
		for(ll 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)
	{
		if(xd>=1&&xd<=n)cout<<xd<<" ";
	}
	cout<<endl;
return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 364 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 492 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 1260 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 9196 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 84 ms 89964 KB Unexpected end of file - int32 expected
2 Halted 0 ms 0 KB -