【二分匹配】 HDU 1528 Card Game Cheater

题意:两个人分别有n张牌 并给出牌的大小关系,用田忌赛马的方式 

求第二个人最多能赢多少张牌;

思路:给每张牌一个值 用二分匹配搞

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 130;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 1101521204;
const int mod = 10000007;
int getval(char *s)
{
    int res;
    if('2'<=s[0]&&s[0]<='9')
        res=(s[0]-'2')*4;
    else
    {
        if(s[0]=='T') res=8*4;
        if(s[0]=='J') res=9*4;
        if(s[0]=='Q') res=10*4;
        if(s[0]=='K') res=11*4;
        if(s[0]=='A') res=12*4;
    }
    if(s[1]=='H') res+=3;
    if(s[1]=='S') res+=2;
    if(s[1]=='D') res+=1;
    return res;
}
int a[44],b[44],numN;
int link[44],used[44];
bool dfs(int u)
{
    for(int i=0;i<numN;i++)
    {
        if(!used[i]&&b[u]>a[i])
        {
            used[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=u;
                return true;
            }
        }
    }
    return false;
}
int findmax()
{
    int res=0;
    cler(link,-1);
    for(int i=0;i<numN;i++)
    {
        cler(used,0);
        if(dfs(i))
            res++;
    }
    return res;
}
int main()
{
    int t,n;
   // IN;
    scanf("%d",&t);
    while(t--)
    {
        char s[4];
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
          scanf("%s",s);
          a[i]=getval(s);
        }
        for(int i=0;i<n;i++)
        {
          scanf("%s",s);
          b[i]=getval(s);
        }
        numN=n;
//        for(int i=0;i<n;i++)
//            printf("%d %d\n",a[i],b[i]);
        printf("%d\n",findmax());
    }
    return 0;
}


posted @ 2014-09-15 19:57  kewowlo  阅读(136)  评论(0编辑  收藏  举报