AT2567 RGB Sequence

对每个右端点, 记录它向左第一次出现某种颜色的位置

(f[i][r][g][b])表示当前考虑到第i位,最后一个r,g,b颜色的出现的位置。

显然有(i=max(r,g,b))

然后就直接(dp)就可以了。

/*
@Date    : 2019-10-05 11:48:12
@Author  : Adscn (adscn@qq.com)
@Link    : https://www.cnblogs.com/LLCSBlog
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IL inline
#define RG register
#define gi geti<int>()
#define gl geti<ll>()
#define gc getchar()
#define File(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
template<typename T>IL bool chkmax(T &x,const T &y){return x<y?x=y,1:0;}
template<typename T>IL bool chkmin(T &x,const T &y){return x>y?x=y,1:0;}
template<typename T>
IL T geti()
{
	RG T xi=0;
	RG char ch=gc;
	bool f=0;
	while(!isdigit(ch))ch=='-'?f=1:f,ch=gc;
	while(isdigit(ch))xi=xi*10+ch-48,ch=gc;
	return f?-xi:xi;
}
template<typename T>
IL void pi(T k,char ch=0)
{
	if(k<0)k=-k,putchar('-');
	if(k>=10)pi(k/10);
	putchar(k%10+'0');
	if(ch)putchar(ch);
}
const int P=1e9+7;
const int N=307;
typedef pair<int,int> pii;
vector<pii>e[N];
int n,m,f[N][N][N];
bool check(int r,int b,int g)
{
	int mx=max(r,max(b,g));
	for(auto&&i:e[mx])
	{
		int l=i.first,x=i.second;
		int tot=0;
		if(r>=l)++tot;
		if(g>=l)++tot;
		if(b>=l)++tot;
		if(tot!=x)return false;
	}
	return true;
}
int main(void)
{
	int n=gi,m=gi;
	for(int i=1;i<=m;++i)
	{
		int l=gi,r=gi,x=gi;
		e[r].push_back(make_pair(l,x));
	}
	ll ans=0;
	f[0][0][0]=1;
	for(int i=0;i<=n;++i)
		for(int j=0;j<=n;++j)
			for(int k=0;k<=n;++k)
			{
				if(!f[i][j][k])continue;
				if((i==j&&i&&j)||(j==k&&j&&k)||(i==k&&i&&k)){f[i][j][k]=0;continue;}
				if(!check(i,j,k))continue;
				int mx=max(i,max(j,k));
				(f[mx+1][j][k]+=f[i][j][k])%=P;
				(f[i][mx+1][k]+=f[i][j][k])%=P;
				(f[i][j][mx+1]+=f[i][j][k])%=P;
				if(mx==n)(ans+=f[i][j][k])%=P;
			}
	pi(ans);
	return 0;
}
原文地址:https://www.cnblogs.com/LLCSBlog/p/11624347.html