AtCoder Grand Contest 009 C - Division into Two

https://atcoder.jp/contests/agc009/tasks/agc009_c

感觉这种题遇到过,就是一堆数字放两个集合,有多少种方法让集合内的差值都大于某个一个固定值

一直都不会做,今天又见到了,但之前遇到的好像是两个集合相同的版本,这个是两个集合不同的固定值,所以两个集合本身也是不同的,也就是 {1},{}和{},{1}是有区别的

不妨设A<=B

设f[i]为前i个划分,s[i]在B集合的方案数

假设f[i]可以由f[j]转移过来,也就是B集合中倒数两个数是s[j],s[i]时,需要满足的条件有s[i]-s[j]>=B, s[j+1,i-1]之间要两两之间大于A

那么用双指针维护一段可以转移的地方就可以了

注意特判如果存在s[i]-s[i-2]

这题细节也挺多的,比如要让s[0]=-1e18,f[0]=1,l=0,r=0开局,才能从空串转移来,然后l,r下标也挺难搞的,状态转移看起来简单,写起来很难受

#include
using namespace std;
typedef long long ll;

const int maxl=3e5+10;
const int mod=1e9+7;

int n,m,k,cnt,tot,cas;ll ans;
ll xa,xb;
ll a[maxl],f[maxl];
bool vis[maxl];
char s[maxl];

inline void prework()
{
	scanf("%d",&n);
	scanf("%lld%lld",&xa,&xb);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	if(xa>xb)
		swap(xa,xb);
}

inline void mainwork()
{
	if(n==1)
	{
		ans=2;
		return;
	}
	for(int i=3;i<=n;i++)
	if(a[i]-a[i-2]=xb)
		{	
			r++;
			if(l<=r)
				(tmp+=f[r])%=mod;
		}
		f[i]=tmp;
		if(a[i]-a[i-1]=0;i--)
	{
		if(a[i+2]-a[i+1]

 

你可能感兴趣的:(DP)