Description

There are two rows of positive integer numbers. We can draw one line segment between any two equal numbers, with values r, if one of them is located in the first row and the other one is located in the second row. We call this line segment an r-matching segment. The following figure shows a 3-matching and a 2-matching segment.

We want to find the maximum number of matching segments possible to draw for the given input, such that:
1. Each a-matching segment should cross exactly one b-matching segment, where a != b.
2. No two matching segments can be drawn from a number. For example, the following matchings are not allowed.

Write a program to compute the maximum number of matching segments for the input data. Note that this number is always even.

Input

The first line of the file is the number M, which is the number of test cases (1 <= M <= 10). Each test case has three lines. The first line contains N1 and N2, the number of integers on the first and the second row respectively. The next line contains N1 integers which are the numbers on the first row. The third line contains N2 integers which are the numbers on the second row. All numbers are positive integers less than 100.

Output

Output file should have one separate line for each test case. The maximum number of matching segments for each test case should be written in one separate line.

Sample Input

3
6 6
1 3 1 3 1 3
3 1 3 1 3 1
4 4
1 1 3 3 
1 1 3 3 
12 11
1 2 3 3 2 4 1 5 1 3 5 10 3
1 2 3 2 4 12 1 5 5 3

Sample Output

6
0
8
 

题目大意:上下2排数据,找一个满足条件的最大匹配数(条件是任意一个匹配的连线都要被至少另一个不一样的匹配穿过)!

解题思路:opt[i][j]为 up[] 数组前 i 个数与 down[] 数组前 j 个数的最大匹配.递推关系:

         opt[i][j] = max{ opt[i-1][j], opt[i][j-1], opt[a-1][b-1] + 2}

      >_< :上式 a,b 的取值须满足 (1 <= a < i) && (1 <= b < j) 并且存在匹配 (up[a] == down[j]) && (down[b] == up[i]) && (up[a] != up[i])

 #include<iostream>
#include<string.h>
using namespace std;
int M;
int N1,N2;
int up[],down[];
int opt[][];
int main(){
cin>>M;
while(M--){
cin>>N1>>N2;
memset(opt,,sizeof(opt));
for(int i=;i<=N1;i++)cin>>up[i];
for(int j=;j<=N2;j++)cin>>down[j]; for(int i=;i<=N1;i++){
for(int j=;j<=N2;j++){
opt[i][j]= opt[i-][j]>opt[i][j-] ? opt[i-][j]:opt[i][j-];
if(up[i]!=down[j]){//只有最后2个不一样时才有可能都和前面的有匹配
int t=;
for(int a=;a<i;a++){
for(int b=;b<j;b++){//遍历查找满足条件的t
if(up[a]==down[j] && up[i]==down[b] && t<opt[a-][b-]+)
t=opt[a-][b-]+;
}
}
opt[i][j]=opt[i][j]>t ? opt[i][j]:t;
}
}
} cout<<opt[N1][N2]<<'\n';
}return ;
}
 
 

[ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)的更多相关文章

  1. zoj 1425 最大交叉匹配

    Crossed Matchings Time Limit: 2 Seconds      Memory Limit: 65536 KB There are two rows of positive i ...

  2. sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)

    Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...

  3. POJ 1692 Crossed Matchings(DP)

    Description There are two rows of positive integer numbers. We can draw one line segment between any ...

  4. POJ1692 Crossed Matchings

    Time Limit: 1000MS     Memory Limit: 10000K Total Submissions: 2738   Accepted: 1777 Description The ...

  5. POJ 1692 Crossed Matchings dp[][] 比较有意思的dp

    http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...

  6. ZOJ 1364 Machine Schedule(二分图最大匹配)

    题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每一个任务能够用A机器的模式i或b机器的模式j来完毕 机器開始都处于模式0 每 ...

  7. ZOJ 3316 Game 一般图最大匹配带花树

    一般图最大匹配带花树: 建图后,计算最大匹配数. 假设有一个联通块不是完美匹配,先手就能够走那个没被匹配到的点.后手不论怎么走,都必定走到一个被匹配的点上.先手就能够顺着这个交错路走下去,最后一定是后 ...

  8. [ACM_模拟] ZOJ 3713 [In 7-bit 特殊输出规则 7bits 16进制]

    Very often, especially in programming contests, we treat a sequence of non-whitespace characters as ...

  9. [ACM_图论] ZOJ 3708 [Density of Power Network 线路密度,a->b=b->a去重]

    The vast power system is the most complicated man-made system and the greatest engineering innovatio ...

随机推荐

  1. B-F 字符串匹配算法

    Brute-Froce 算法是串的匹配模式算法中的一种其匹配方式如下: 1.设有字符串 a ,b;a为主串,在 a 中查找 b 串的位置 2.匹配方式如下: 2.1: 分别从 a,b串的第一个元素开始 ...

  2. session基础

    1.每个页面都必须开启session_start()后才能在每个页面里面使用session. 2.session_start()初始化session,第一次访问会生成一个唯一会话ID保存在客户端(是基 ...

  3. TortoiseSVN-1.8.11 安装时弹出2503错误导致安装失败解决办法

    这个问题主要是由于msi格式文件在win8中默认不是以管理员身份运行造成,可通过命令行解决: 右键单击win8左下角启动图标,选择命令提示符(管理员): 输入:msiexec /package 要安装 ...

  4. Allegro学习(http://www.asmyword.com/forum.php?mod=forumdisplay&fid=86)

    一.资源 1.网站推荐www.eda365.com,里面有很多有用的东西:当然还有官方代理商的网站http://www.pspice.com.cn/: 2.视频教程:有库源电气的视频教程,还有在www ...

  5. 《Linux内核设计与实现》读书笔记 第二章 从内核出发

    一.获取内核源码 1. Git git实际上是一种开源的分布式版本控制工具. Linux作为一个开源的内核,其源代码也可以用git下载和管理 - 获取最新提交到版本树的一个副本 - $ git clo ...

  6. computer repair services in Hangzhou

    We provide support for all kinds of Windows based Desktops and Laptops all over Hangzhou,I will be i ...

  7. 【BZOJ1226】学校食堂Dining(状压DP)

    题意:见题面 思路:设dp[i,sta,k]为前i个人已经吃完,从第i人到第i+b[i]人的吃饭状况是sta,前一个吃完的人离i的距离是k(可能为负)的最小值 \[ dp[i+1,sta>> ...

  8. 如何使用本地yum源?

    首先为大家介绍在Centos系统上如何利用系统光盘/镜像作为yum源,实现程序包的安装等操作 1.首先在VM虚拟机上确保已载入光盘镜像,载入成功后显示如下效果. 2.挂载光盘镜像文件,使用命令: mo ...

  9. 也说面试 - 一个努力的iOS Dev

    你们在金色的余晖中回家,而我却在银色的温柔中,匆匆潜行-----这是我的现状. 今年的招工形式不是很好,难找工作:也难招人.写这篇博客,是为了给各位在找工作的iOS dev 一些参考. 上篇:换坑(去 ...

  10. 使用 BeanCopier 复制对象

    Cglib是一款比较底层的操作java字节码的框架. BeanCopier是一个工具类,可以用于Bean对象内容的复制. 复制Bean对象内容的方法有很多,比如自己手动get set ,或者使用Pro ...