题意:Adam和Eve玩扑克,比大小,“23456789TJQKA”从前到后依次增大,“CDSH”四种花色依次增大,谁的牌大,谁就得一分。Eve开始就知道Adam的所有牌,Eve可以选择拿什么牌对Adam的什么牌。问:Eve最多能得几分?
思路:建图,对Eve的第i张牌,枚举Adam的牌,如果Adam的牌比Eve的牌小的话,就连一条边。求最大匹配即可。(这不就是田忌赛马吗?也就是可以贪了。)
http://acm.hdu.edu.cn/showproblem.php?pid=1528
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = a ; i <= b ; i ++)
#define rrep(i,a,b) for(int i = b ; i >= a ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
using namespace std;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5;
const int MAXE = 2e5;
typedef long long LL;
typedef unsigned long long ULL;
struct Edge {
int to;
Edge * next;
}E[MAXE],*EE;
struct Gragh {
Edge * first;
}G[MAXN];
int N,M;
bool visit[MAXN];
int match[MAXN];
void addedge(int u,int v) {
EE->to = v ; EE -> next = G[u].first ; G[u].first = EE ++;
}
int T,n,m;
void init() {
EE = E; N = M = 0;
cls(G,0);
}
bool find_path(int u) {
int v;
repE(p,u) {
v = p->to;
if(!visit[v]) {
visit[v] = 1;
if(match[v] == -1 || find_path(match[v])) {
match[v] = u;
return true;
}
}
}
return false;
}
int Max_match() {
cls(match,-1);
int cnt = 0;
rep(i,1,N) {
cls(visit,0);
if(find_path(i)) cnt ++;
}
return cnt;
}
char s1[15] = "023456789TJQKA";
char h1[10] = "0CDSH";
char s2[300];
char h2[300];
void init_i() {
rep(i,1,13) {
s2[s1[i]] = i;
}
rep(i,1,4) {
h2[h1[i]] = i;
}
}
bool cmp(char * a1,char * a2) {
if(s2[a1[0]] == s2[a2[0]]) return h2[a1[1]] > h2[a2[1]];
return s2[a1[0]] > s2[a2[0]];
}
char a[30][3];
char b[30][3];
void input() {
scanf("%d",&n);
rep(i,1,n) scanf("%s",a[i]);
rep(i,1,n) scanf("%s",b[i]);
}
void solve() {
N = n;
M = n;
rep(i,1,n) {
rep(j,1,n) {
if(cmp(b[i],a[j])) addedge(i,j+N);
}
}
printf("%d\n",Max_match());
}
int main(void) {
init_i();
scanf("%d",&T); while(T--) {
init();
input();
solve();
}
return 0;
}