标签:include == MIXED 表达 cow char 状压 ble etc
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
int a[20];ll ans,f[20][100005];//记得开long long
int main(){
int n=read(),m=read();
for(int i=1;i<=n;++i)a[i]=read();
for(int i=1;i<=n;++i)f[i][1<<(i-1)]=1;
/*for(int i=1;i<=n;++i){
for(int j=1;j<(1<<n);++j){
if(j&(1<<(i-1))){
for(int k=1;k<=n;++k){
if(k==i||abs(a[k]-a[i])<=m)continue;
if(j&(1<<(k-1)))f[i][j]+=f[k][j^(1<<(i-1))];
}
}
}
}*/
//上面是我刚开始写的代码,搞了一个多小时,样例都过不去
//然后去看题解,就换一个i,j的枚举顺序就行了,不多说了,菜是原罪.
//然后仔细想想,一般状压DP,都是先枚举的集合状态是吧.
//这样才能保证对于一个集合j,所有能够转移到它的集合j'都已经被更新完了
//就是说上面那种写法,更新f[i][j]+=f[k][j^(1<<(i-1))]的时候
//当k>i时,f[k][j^(1<<(i-1))]是还没有被更新的.
for(int j=1;j<(1<<n);++j){
for(int i=1;i<=n;++i){
if(j&(1<<(i-1))){
for(int k=1;k<=n;++k){
if(abs(a[k]-a[i])<=m)continue;
if(j&(1<<(k-1)))f[i][j]+=f[k][j^(1<<(i-1))];
}
}
}
}
for(int i=1;i<=n;++i)ans+=f[i][(1<<n)-1];
printf("%lld\n",ans);
return 0;
}
[USACO08NOV]奶牛混合起来Mixed Up Cows
标签:include == MIXED 表达 cow char 状压 ble etc
原文地址:https://www.cnblogs.com/PPXppx/p/11724839.html