【jzoj4325】【NOIP2015提高组Day1】【斗地主】

题目大意

给出一副手牌,按斗地主的规则最少多少次打完。

解题思路

暴力枚举,先打顺子,再打四带,再打三带,最后打对子和单牌。

code

#include
#include
#include
#include
#include
#define LF double
#define LL long long
#define max(n1,n2) ((n1>n2)?n1:n2)
#define min(n1,n2) ((n1>n2)?n2:n1)
#define num(n1,n2) ((n1-1)*3*n+n2+1)
#define fo(i,j,k) for(int i=j;i<=k;i++)
#define fd(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
int const maxn=100,inf=2147483647;
int t,n,ans,num[20],sum[10];
void dfs(int now){
    fo(j,2,12)
        fo(i,3,14-j+1){
            int mi=3;
            fo(k,i,i+j-1)mi=min(mi,num[k]);
            if((mi>=3)&&(j>=2)){
                fo(k,i,i+j-1)num[k]-=3;
                dfs(now+1);
                fo(k,i,i+j-1)num[k]+=3;
            }
            if((mi>=2)&&(j>=3)){
                fo(k,i,i+j-1)num[k]-=2;
                dfs(now+1);
                fo(k,i,i+j-1)num[k]+=2;
            }
            if((mi>=1)&&(j>=5)){
                fo(k,i,i+j-1)num[k]--;
                dfs(now+1);
                fo(k,i,i+j-1)num[k]++;
            }
        }
    fo(i,0,4)sum[i]=0;
    fo(i,0,14)sum[num[i]]++;
    //now+=(num[0]!=0);
    int tmp=min(sum[1]/2,sum[4]);now+=tmp;
    sum[1]-=tmp*2;sum[4]-=tmp;
    tmp=min(sum[1],sum[3]);now+=tmp;
    sum[1]-=tmp;sum[3]-=tmp;
    tmp=min(sum[2]/2,sum[4]);now+=tmp;
    sum[2]-=tmp*2;sum[4]-=tmp;
    tmp=min(sum[2],sum[3]);now+=tmp;
    sum[2]-=tmp;sum[3]-=tmp;
    if(sum[4]&&sum[3]){
        tmp=min(sum[4]/2,sum[3]/2);now+=tmp*2;
        sum[4]-=tmp*2;sum[3]-=tmp*2;
        tmp=min(sum[4]/3,sum[3]);now+=tmp*3;
        sum[4]-=tmp*3;sum[3]-=tmp;
        ans=min(ans,now+sum[3]+sum[4]);
    }else if(sum[4]){
        tmp=sum[4]/2;now+=tmp;
        sum[4]-=tmp*2;
        tmp=min(sum[4],sum[2]);now+=tmp;
        sum[4]-=tmp;sum[2]-=tmp;
        ans=min(ans,now+sum[1]+sum[2]+sum[4]);
    }else if(sum[3])ans=min(ans,now+sum[3]);
    else ans=min(ans,now+sum[1]+sum[2]);
}
int main(){
    //freopen("landlords.in","r",stdin);
    //freopen("landlords.out","w",stdout);
    freopen("d.in","r",stdin);
    freopen("d.out","w",stdout);
    scanf("%d%d",&t,&n);
    fo(cas,1,t){
        fo(i,0,14)num[i]=0;
        fo(i,1,n){
            int a,b;scanf("%d%d",&a,&b);
            if(a!=1)num[a]++;
            else num[14]++;
        }
        if(cas==65){
            int bb;
            bb++;
        }
        ans=n;
        dfs(0);
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(jzoj,搜索)