# dp 杂练/专练

bzoj1055$bzoj1055$
dp[i][j][k]ijk$dp[i][j][k]表示i到j能否缩成k$

//Copyright(c)2016 liuchenrui
#include<bits/stdc++.h>
#define N 255
using namespace std;
char p[]={'W','I','N','G'},s[N],a[4][20][3];
int q[N],t[4],dp[N][N][4];
bool dodp(int l,int r,int k){
if(l==r)return s[l]==p[k];
int &res=dp[l][r][k];
if(res!=-1)return res;
for(int i=1;i<=t[k];i++)
for(int j=l;j<=r-1;j++)
if(dodp(l,j,q[a[k][i][0]])&&dodp(j+1,r,q[a[k][i][1]]))
return res=1;
return res=0;
}
int main(){
freopen("xxx.in","r",stdin);
freopen("xxx.out","w",stdout);
memset(dp,-1,sizeof(dp));
q['W']=0;q['I']=1;q['N']=2;q['G']=3;
for(int i=0;i<4;i++){
cin>>t[i];
}
for(int i=0;i<4;i++){
for(int j=1;j<=t[i];j++){
scanf("%s",a[i][j]);
}
}
scanf("%s",s+1);int n=strlen(s+1);
bool flag=0;
for(int i=0;i<4;i++){
if(dodp(1,n,i)){
flag=1,printf("%c",p[i]);
}
}
if(!flag)puts("The name is wrong!");
}

bzoj1090$bzoj 1090$
dp[i][j]ij$dp[i][j]表示i到j的最小距离$
n3$n^3枚举，暴力判断即可$

//Copyright(c)2016 liuchenrui
#include<bits/stdc++.h>
#define inf 1000000000
#define N 105
using namespace std;
int dp[N][N],n;
char s[N];
int get(int x){
if(x<10)return 1;
if(x==100)return 3;
return 2;
}
int calc(int l1,int r1,int l2,int r2){
if((r2-l2+1)%(r1-l1+1))return inf;
for(int i=l2,j=l1;i<=r2;i++,j++){
if(j==r1+1)j=l1;
if(s[i]!=s[j])return inf;
}
return dp[l1][r1]+2+get((r2-l2+1)/(r1-l1+1)+1);
}
int main(){
freopen("xxx.in","r",stdin);
freopen("xxx.out","w",stdout);
scanf("%s",s+1);n=strlen(s+1);
memset(dp,63,sizeof dp);
for(int i=1;i<=n;i++){
dp[i][i]=1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n-i+1;j++){
int l=j,r=i+j-1;
for(int k=l;k<r;k++){
dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);
dp[l][r]=min(dp[l][r],calc(l,k,k+1,r));
}
}
}
cout<<dp[1][n]<<endl;
}

bzoj1334$bzoj1334$

a使sum(a)sum(tot)a[i]sum(a)a[i]sum(tot)$题意：给一个集合，选出一个子集a，使得这个子集和sum(a)比全部的和的一半sum(tot)大，并且对于任意子集内的元素a[i]，sum(a)-a[i]比sum(tot)小$

//Copyright(c)2016 liuchenrui
#include<bits/stdc++.h>
using namespace std;
inline void splay(int &v){
v=0;char c=0;int p=1;
while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
v*=p;
}
int dp[100005],a[305],sum,ans,n;
int main(){
//freopen("xxx.in","r",stdin);
//freopen("xxx.out","w",stdout);
splay(n);
for(int i=1;i<=n;i++)splay(a[i]),sum+=a[i];
sort(a+1,a+n+1);dp[0]=1;
for(int i=n;i>=1;i--){
for(int j=sum/2+a[i];j>=a[i];j--){
if(dp[j-a[i]]){
dp[j]=1;ans=max(j,ans);
}
}
}
printf("%d",ans);
}

bzoj3687$bzoj 3687$
interesting$interesting$
dp[i]i$dp[i]表示i能否达到$
xor$相同的数xor会抵消$

//Copyright(c)2016 liuchenrui
#include<bits/stdc++.h>
using namespace std;
inline void splay(int &v){
v=0;char c=0;int p=1;
while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
v*=p;
}
bitset<2000001>a;
int main(){
int n;splay(n);int x,ans=0;a[0]=1;
for(;n--;)scanf("%d",&x),a^=a<<x;
for(int i=1;i<=2000000;i++)if(a[i])ans^=i;
cout<<ans<<endl;
}

bzoj3573$bzoj3573$
$这道题被抓去直播写代码了$QAQ
$一个权值确定会带领全图的权值确定$
p[1]=0,p[x]=(11)$令p[1]=0,p[x]=(这个点在1点为1的情况下权值为多少)$
log$然后每个点反过来取log$
n$然后排序，记录相同的权值，答案为n-最多的权值个数$

//Copyright(c)2016 liuchenrui
#include<bits/stdc++.h>
using namespace std;
inline void splay(int &v){
v=0;char c=0;int p=1;
while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
v*=p;
}
#define N 500010
int nxt[N*2],fir[N],to[N*2],a[N],sz,d[N],n;
double p[N];
nxt[++sz]=fir[x],fir[x]=sz,to[sz]=y;
}
void dfs(int x,int fa){
for(int u=fir[x];u;u=nxt[u]){
if(to[u]!=fa){
p[to[u]]=p[x]+log(double(d[x]));
dfs(to[u],x);
}
}
}
int main(){
freopen("xxx.in","r",stdin);
freopen("xxx.out","w",stdout);
splay(n);
for(int i=1;i<=n;i++){
splay(a[i]);
}
for(int i=1;i<n;i++){
int x,y;splay(x),splay(y);
d[x]++,d[y]++;
}
for(int i=2;i<=n;i++)d[i]--;
p[1]=0;dfs(1,0);
for(int i=1;i<=n;i++){
p[i]+=log(double(a[i]));
}
sort(p+1,p+n+1);
int now=1,ans=0;
for(int i=1;i<=n;i++){
if(p[i]-p[now]>1e-5){
ans=max(ans,i-now);
now=i;
}
}
ans=max(ans,n-now+1);
cout<<n-ans<<endl;
}

bzoj1369$bzoj 1369$
clj105$据clj讲，这题颜色不会超过10，我开到5就够了$
dp[i][j]ij$dp[i][j]表示i点，j颜色最小代价$
yy$剩下自己yy就能做出来辣$

//Copyright(c)2016 liuchenrui
#include<bits/stdc++.h>
#define N 100010
using namespace std;
inline void splay(int &v){
v=0;char c=0;int p=1;
while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
v*=p;
}
int dp[N][6],n,fir[N],nxt[N*2],to[N*2],sz;
nxt[++sz]=fir[x],fir[x]=sz,to[sz]=y;
}
void dfs(int x,int fa){
for(int u=fir[x];u;u=nxt[u]){
if(to[u]==fa)continue;
dfs(to[u],x);
}
for(int i=1;i<=5;i++){
int cnt=i;
for(int u=fir[x];u;u=nxt[u]){
if(to[u]==fa)continue;
int mi=1<<28;
for(int j=1;j<=5;j++){
if(i!=j){
mi=min(mi,dp[to[u]][j]);
}
}
cnt+=mi;
}
dp[x][i]=cnt;
}
}
int main(){
freopen("xxx.in","r",stdin);
freopen("xxx.out","w",stdout);
splay(n);
for(int i=1,x,y;i<n;i++){
splay(x),splay(y);
}