Submission #100228

#TimeUsernameProblemLanguageResultExecution timeMemory
100228TadijaSebezSplit the sequence (APIO14_sequence)C++11
100 / 100
453 ms82988 KiB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define ldb long double
const int N=100050;
const int K=205;
const ll inf=1e18;
struct Line{ ll k,n;int id;Line(){}Line(ll a, ll b, int c):k(a),n(b),id(c){}};
int hsz,ptr=1;
ll dp[N],sum[N];
int go[K][N],a[N];
void Print(int n, int k)
{
	if(k==0) return;
	Print(go[k][n],k-1);
	printf("%i ",go[k][n]);
}
int main()
{
	int n,k;
	scanf("%i %i",&n,&k);
	for(int i=1;i<=n;++i) scanf("%i",&a[i]),sum[i]=sum[i-1]+a[i];
	static Line Hull[N];
	for(int j=1;j<=k;++j)
	{
		hsz=0;ptr=1;
		Hull[++hsz]=Line(sum[j],dp[j]-sum[j]*sum[j],j);
		for(int i=j+1;i<=n;++i)
		{
			Line l=Line(sum[i],dp[i]-sum[i]*sum[i],i);
			while(ptr<hsz && Hull[ptr].k*sum[i]+Hull[ptr].n<=Hull[ptr+1].k*sum[i]+Hull[ptr+1].n) ++ptr;
			dp[i]=Hull[ptr].k*sum[i]+Hull[ptr].n;
			go[j][i]=Hull[ptr].id;
			while(hsz>ptr && (l.n-Hull[hsz].n)*(Hull[hsz].k-Hull[hsz-1].k)>=(Hull[hsz].n-Hull[hsz-1].n)*(l.k-Hull[hsz].k)) --hsz;
			Hull[++hsz]=l;
		}
	}
	printf("%lld\n",dp[n]);
	Print(n,k);
	return 0;
}

Compilation message (stderr)

sequence.cpp: In function 'int main()':
sequence.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i",&n,&k);
  ~~~~~^~~~~~~~~~~~~~~
sequence.cpp:23:41: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;++i) scanf("%i",&a[i]),sum[i]=sum[i-1]+a[i];
                        ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
#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...