bzoj 3613: [Heoi2014]南园满地堆轻絮【二分+贪心】

二分答案w,然后判断的时候维护一个mx,扫描序列,先更新mx=max(mx,a[i]-w),然后如果a[i]+w<mx的话就是说这个位置即使升到极限并且前面降到极限也不能符合条件了

#include<iostream>
#include<cstdio>
using namespace std;
const int N=5000005;
int n,sa,sb,sc,sd,a[N],mod;
int clc(int x)
{
	return (((1ll*sa*x%mod*x%mod*x%mod+1ll*sb*x%mod*x%mod)%mod+1ll*sc*x%mod)+1ll*sd)%mod;
}
bool ok(int w)
{
	int mx=1;
	for(int i=1;i<=n;i++)
	{
		mx=max(mx,a[i]-w);
		if(mx>a[i]+w) 
			return 0;
	}
	return 1;
}
int main()
{
	scanf("%d%d%d%d%d%d%d",&n,&sa,&sb,&sc,&sd,&a[1],&mod);
	for(int i=2;i<=n;i++)
		a[i]=(clc(a[i-2])+clc(a[i-1]))%mod;
	int l=0,r=mod,ans=mod;
	while(l<=r)
	{
		int mid=(l+r)>>1;
		if(ok(mid))
			r=mid-1,ans=mid;
		else
			l=mid+1;
	}
	printf("%d\n",ans);
	return 0;
}
posted @ 2018-09-26 17:43  lokiii  阅读(89)  评论(0编辑  收藏  举报