zoukankan      html  css  js  c++  java
  • HDU-1528-Card Game Cheater(二分图匹配)

    链接:https://vjudge.net/problem/HDU-1528

    题意:

    Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the cards face down in a row on the table. Adam’s cards are numbered from 1 to k from his left, and Eve’s cards are numbered 1 to k from her right (so Eve’s i:th card is opposite Adam’s i:th card). The cards are turned face up, and points are awarded as follows (for each i ∈ {1, . . . , k}): 
    If Adam’s i:th card beats Eve’s i:th card, then Adam gets one point. 
    If Eve’s i:th card beats Adam’s i:th card, then Eve gets one point. 
    A card with higher value always beats a card with a lower value: a three beats a two, a four beats a three and a two, etc. An ace beats every card except (possibly) another ace. 
    If the two i:th cards have the same value, then the suit determines who wins: hearts beats all other suits, spades beats all suits except hearts, diamond beats only clubs, and clubs does not beat any suit. 
    For example, the ten of spades beats the ten of diamonds but not the Jack of clubs. 
    This ought to be a game of chance, but lately Eve is winning most of the time, and the reason is that she has started to use marked cards. In other words, she knows which cards Adam has on the table before he turns them face up. Using this information she orders her own cards so that she gets as many points as possible. 

    Your task is to, given Adam’s and Eve’s cards, determine how many points Eve will get if she plays optimally. 

    a和b有n张牌,求最多有几组b的牌比a的牌大。

    思路:

    二分图匹配,最大匹配。

    建图比较烦,我是用结构体先记录牌,再比大小建图,最后直接最大匹配。

    代码:

    #include <iostream>
    #include <memory.h>
    #include <string>
    #include <istream>
    #include <sstream>
    #include <vector>
    #include <stack>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <math.h>
    #include <cstdio>
    #include <set>
    #include <iterator>
    #include <cstring>
    using namespace std;
    
    typedef long long LL;
    const int MAXN = 1e3+10;
    int Next[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
    
    struct Node
    {
        string _card;
        int l;
        int r;
        void Init()
        {
            if (isdigit(_card[0]))
                l = _card[0] - '0';
            else if (_card[0] == 'T')
                l = 10;
            else if (_card[0] == 'J')
                l = 11;
            else if (_card[0] == 'Q')
                l = 12;
            else if (_card[0] == 'K')
                l = 13;
            else
                l = 14;
            if (_card[1] == 'H')
                r = 4;
            else if (_card[1] == 'S')
                r = 3;
            else if (_card[1] == 'D')
                r = 2;
            else
                r = 1;
        }
        bool operator > (const Node & that) const
        {
            if (this->l  != that.l)
                return this->l > that.l;
            return this->r > that.r;
        }
    }Adam[30], Eve[30];
    
    vector<int> G[MAXN];
    int Dis[200][200];
    int Link[MAXN], Vis[MAXN];
    int n, m, k;
    int mid;
    
    bool Dfs(int x)
    {
        for (int i = 0;i < G[x].size();i++)
        {
            int node = G[x][i];
            if (Vis[node] == 0)
            {
                Vis[node] = 1;
                if (Link[node] == -1 || Dfs(Link[node]))
                {
                    Link[node] = x;
                    return true;
                }
            }
        }
        return false;
    }
    
    int Solve()
    {
        memset(Link, -1, sizeof(Link));
        int cnt = 0;
        for (int i = 1;i <= n;i++)
        {
            memset(Vis, 0, sizeof(Vis));
            if (Dfs(i))
                cnt++;
        }
        return cnt;
    }
    
    int main()
    {
        int t;
        cin >> t;
        while (t--)
        {
            cin >> n;
            for (int i = 1;i <= n;i++)
                G[i].clear();
            for (int i = 1;i <= n;i++)
                cin >> Adam[i]._card, Adam[i].Init();
            for (int i = 1;i <= n;i++)
                cin >> Eve[i]._card, Eve[i].Init();
            for (int i = 1;i <= n;i++)
            {
                for (int j = 1;j <= n;j++)
                {
                    if (Eve[i] > Adam[j])
                    {
                        G[i].push_back(j);
                    }
                }
            }
            int res = Solve();
            cout << res << endl;
        }
    
        return 0;
    }
    

      

  • 相关阅读:
    java几种基本排序算法
    Java 数组
    java自增(自减)运算符
    java数据类型
    java变量
    java注释
    c语言数字图像处理(十):阈值处理
    c语言数字图像处理(九):边缘检测
    c语言数字图像处理(五):空间滤波
    c语言数字图像处理(八):噪声模型及均值滤波器
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10871846.html
Copyright © 2011-2022 走看看