博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1692 Crossed Matchings
阅读量:7070 次
发布时间:2019-06-28

本文共 2878 字,大约阅读时间需要 9 分钟。

 

Time Limit: 1000MS

    Memory Limit: 10000K
Total Submissions: 2738   Accepted: 1777

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 input 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 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

36 61 3 1 3 1 33 1 3 1 3 14 41 1 3 3 1 1 3 3 12 111 2 3 3 2 4 1 5 1 3 5 103 1 2 3 2 4 12 1 5 5 3

Sample Output

608

Source

 

两个交叉的匹配为一组,每找到一组可行的匹配,答案数+2 。

设:f[上方匹配位置][下方匹配位置]=最优解

假设现在扫到了上方数组的i点和下方数组的j点。首先可以想到如果没有新的匹配,f[i][j]=max(f[i][j-1],f[i-1][j])

接着考虑新的匹配,在上方数组中从i往前找,找到最近的pos1使a[pos1]=b[j],同理在下方找到b[pos2]=a[i],那么pos1-j,pos2-i两条连线必然交叉,得到动归方程:

f[i][j]=max(f[i][j],f[pos1-1][pos2-1]+2)

 

1 /**/ 2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 const int mxn=120; 9 int n1,n2;10 int a[mxn],b[mxn];11 int f[mxn][mxn];12 int main(){13 int T;14 scanf("%d",&T);15 int i,j;16 while(T--){17 memset(f,0,sizeof f);18 scanf("%d%d",&n1,&n2);19 for(i=1;i<=n1;i++)scanf("%d",&a[i]);20 for(i=1;i<=n2;i++)scanf("%d",&b[i]);21 for(i=1;i<=n1;i++)22 for(j=1;j<=n2;j++){23 f[i][j]=max(f[i][j-1],f[i-1][j]);24 if(a[i]==b[j])continue;25 int k=i-1;26 while(k && a[k]!=b[j])k--;int pos1=k;27 k=j-1;28 while(k && b[k]!=a[i])k--;int pos2=k;29 if(pos1&&pos2) f[i][j]=max(f[i][j],f[pos1-1][pos2-1]+2);30 }31 printf("%d\n",f[n1][n2]);32 }33 return 0;34 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5737904.html

你可能感兴趣的文章
[译] 一、为何要推出AppCoda系列?
查看>>
HDU4685 Prince and Princess 完美搭配+良好的沟通
查看>>
Spring AOP在pointcut expression解析表达式 并匹配多个条件
查看>>
建立完整的单向动态链表(包括初始化、创建、插入、删除、查找、销毁、输出)...
查看>>
如果有一天你没有了动力,可以看看
查看>>
选择算法
查看>>
反射获取指定类型
查看>>
springJDBC一对多关系,以及Java递归,jsp递归的实现
查看>>
Codeforces Gym 100733A Shitália 计算几何
查看>>
configure: error: png.h not found.
查看>>
学习笔记: JavaScript/JQuery 的cookie操作
查看>>
clearcase 中一些概念和操作
查看>>
Linux互斥和同步应用程序(一):posix线程和线程之间的相互排斥
查看>>
iOS tableview上放textfield
查看>>
Bash 中为 _ 变量赋空值的三个场景
查看>>
导入myeclipse项目出现的问题及解决方案
查看>>
java环境变量设置
查看>>
为Redmine的项目加上起止时间
查看>>
win7系统中任务计划程序的使用与查询
查看>>
站在OC的基础上快速理解Swift的类与结构体
查看>>