[ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)

简介:


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])

 

复制代码
 1 #include<iostream>
 2 #include<string.h>
 3 using namespace std;
 4 int M;
 5 int N1,N2;
 6 int up[105],down[105];
 7 int opt[105][105];
 8 int main(){
 9     cin>>M;
10     while(M--){
11         cin>>N1>>N2;
12         memset(opt,0,sizeof(opt));
13         for(int i=1;i<=N1;i++)cin>>up[i];
14         for(int j=1;j<=N2;j++)cin>>down[j];
15 
16         for(int i=1;i<=N1;i++){
17             for(int j=1;j<=N2;j++){
18                 opt[i][j]= opt[i-1][j]>opt[i][j-1] ? opt[i-1][j]:opt[i][j-1];
19                 if(up[i]!=down[j]){//只有最后2个不一样时才有可能都和前面的有匹配
20                     int t=0;
21                     for(int a=1;a<i;a++){
22                         for(int b=1;b<j;b++){//遍历查找满足条件的t
23                              if(up[a]==down[j] && up[i]==down[b] && t<opt[a-1][b-1]+2)
24                                 t=opt[a-1][b-1]+2;
25                         }
26                     }
27                     opt[i][j]=opt[i][j]>t ? opt[i][j]:t;
28                 }
29             }
30         }
31 
32         cout<<opt[N1][N2]<<'\n';
33     }return 0;
34 }
复制代码


相关文章
|
6月前
|
算法 C++
剑指offer(C++)-JZ70:矩形覆盖(算法-动态规划)
剑指offer(C++)-JZ70:矩形覆盖(算法-动态规划)
|
8月前
|
算法 Android开发
LeetCode 周赛上分之旅 #38 结合排序不等式的动态规划
学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越抽象,它能覆盖的问题域就越广,理解难度也更复杂。在这个专栏里,小彭与你分享每场 LeetCode 周赛的解题报告,一起体会上分之旅。
60 0
|
10月前
|
机器学习/深度学习 存储 算法
回溯法求解N皇后问题
回溯法求解N皇后问题
84 0
|
10月前
动态规划:分组背包问题
动态规划:分组背包问题
62 0
|
算法
《趣学算法-动态规划-0-1背包问题》阅读笔记
《趣学算法-动态规划-0-1背包问题》阅读笔记
106 0
《趣学算法-动态规划-0-1背包问题》阅读笔记
|
人工智能 算法
ACM算法训练【双指针算法合集】
思路: 找到i,j的单调性,统一向后移动,使时间复杂度为O(2n) 枚举i,每次看j是否需要向后走,得到最长的长度
73 1
ACM算法训练【双指针算法合集】
|
人工智能 算法
【动态规划】矩阵连乘
完全加括号的矩阵连乘积可递归地定义为: • 单个矩阵是完全加括号的 • 矩阵连乘积A是完全加括号的,则A可表示为2个完全加括号的矩阵连乘积B和C 的乘积并加括号,即A=(BC) 设有四个矩阵A, B, C, D ,它们的维数分别是: A = 50*10 B = 10*40 C = 40*30 D = 30*5 总共有五种完全加括号的方式:
273 0
|
算法
ACM知识 - 贪心算法和动态规划的区别与联系
ACM知识 - 贪心算法和动态规划的区别与联系
156 0
|
存储 算法
矩阵连乘(动态规划)
题目描述:给定n个矩阵{A1,A2,…,An},其中Ai与Ai+1是可乘的,i=1,2 ,…,n-1。如何确定计算矩阵连乘积的计算次序,使得依此次序计算矩阵连乘积需要的数乘次数最少。
3244 0