P3622 [APIO2007]动物园
题目大意:一个环上包含
我也是看了题目标签才会做的。
思路解析:这道题因为只有每个人只能看到
但是当到转移时就会遇到一点困难,那就是如果要取一个
所以
其中
公式解释:其中
最后时间复杂度
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int NR=1e5+10;
const int MR=(1<<6)+1;
int n,m;
int dp[NR][MR];
int f[NR][MR];
int ans;
int read()
{
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*f;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=m;i++)
{
int a=read(),b=read(),c=read();
int st1=0,st2=0;
for(int j=1;j<=b;j++)
{
int x=read();
x=(x-a+n)%n;
st1|=(1<<x);
}
for(int j=1;j<=c;j++)
{
int x=read();
x=(x-a+n)%n;
st2|=(1<<x);
}
for(int st=0;st<=31;st++)
{
f[a][st]+=((st1&(st^31))||(st2&st));
}
}
for(int st1=0;st1<=31;st1++)
{
memset(dp,-999999,sizeof(dp));
dp[0][st1]=0;
for(int i=1;i<=n;i++)
{
for(int st=0;st<=31;st++)
{
dp[i][st]=max(dp[i-1][(st&15)<<1],dp[i-1][(st&15)<<1|1])+f[i][st];
}
}
ans=max(ans,dp[n][st1]);
}
printf("%d",ans);
return 0;
}