[ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)
2014-02-27 18:50
357 查看
[b]Description[/b]
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.
[b]Input[/b]
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.
[b]Output[/b]
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.
[b]Sample Input[/b]
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
[b]Sample Output[/b]
6
0
8
[b]题目大意:上下2排数据,找一个满足条件的最大匹配数(条件是任意一个匹配的连线都要被至少另一个不一样的匹配穿过)![/b]
[b]解题思路:opt[i][j]为 up[] 数组前 i 个数与 down[] 数组前 j 个数的最大匹配.递推关系:[/b]
[b] opt[i][j] = max{ opt[i-1][j], opt[i][j-1], opt[a-1][b-1] + 2}[/b]
[b] >_< :上式 a,b 的取值须满足 (1 <= a < i) && (1 <= b < j) 并且存在匹配 (up[a] == down[j]) && (down[b] == up[i]) && (up[a] != up[i])[/b]
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.
[b]Input[/b]
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.
[b]Output[/b]
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.
[b]Sample Input[/b]
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
[b]Sample Output[/b]
6
0
8
[b]题目大意:上下2排数据,找一个满足条件的最大匹配数(条件是任意一个匹配的连线都要被至少另一个不一样的匹配穿过)![/b]
[b]解题思路:opt[i][j]为 up[] 数组前 i 个数与 down[] 数组前 j 个数的最大匹配.递推关系:[/b]
[b] opt[i][j] = max{ opt[i-1][j], opt[i][j-1], opt[a-1][b-1] + 2}[/b]
[b] >_< :上式 a,b 的取值须满足 (1 <= a < i) && (1 <= b < j) 并且存在匹配 (up[a] == down[j]) && (down[b] == up[i]) && (up[a] != up[i])[/b]
#include<iostream> #include<string.h> using namespace std; int M; int N1,N2; int up[105],down[105]; int opt[105][105]; int main(){ cin>>M; while(M--){ cin>>N1>>N2; memset(opt,0,sizeof(opt)); for(int i=1;i<=N1;i++)cin>>up[i]; for(int j=1;j<=N2;j++)cin>>down[j]; for(int i=1;i<=N1;i++){ for(int j=1;j<=N2;j++){ opt[i][j]= opt[i-1][j]>opt[i][j-1] ? opt[i-1][j]:opt[i][j-1]; if(up[i]!=down[j]){//只有最后2个不一样时才有可能都和前面的有匹配 int t=0; for(int a=1;a<i;a++){ for(int b=1;b<j;b++){//遍历查找满足条件的t if(up[a]==down[j] && up[i]==down[b] && t<opt[a-1][b-1]+2) t=opt[a-1][b-1]+2; } } opt[i][j]=opt[i][j]>t ? opt[i][j]:t; } } } cout<<opt[N1][N2]<<'\n'; }return 0; }
相关文章推荐
- kali&BT安装好之后无法上网或者无法获得内网IP
- win7下eclipse集成hadoop1.0.3
- 字节序相关--本地字节序和网络字节序等
- [JPA] javax.persistence.EntityNotFoundException处理
- NStimer APIhttp://hi.baidu.com/douxinchun/item/920a7655fe7f3fd8d48bacaf
- ubuntu中chown设置文件权限
- C#Regex类的用法
- C++一些注意点之字符串与指针
- 项目3--日期结构体
- 迅雷2014校园招聘笔试题
- python扩展和嵌入
- uva12232 - Exclusive-OR 加权并查集
- 【转】递归生成集合的所有组合
- Linux 学习之编译安装Apache
- vim的使用
- json解析
- Object-C反射读取实体属性和值
- CocoaPods一个Objective-C第三方库的管理利器
- Hive查询在MapReduce上的执行过程
- access-list(访问控制列表)