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

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

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)

 /**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
int n1,n2;
int a[mxn],b[mxn];
int f[mxn][mxn];
int main(){
int T;
scanf("%d",&T);
int i,j;
while(T--){
memset(f,,sizeof f);
scanf("%d%d",&n1,&n2);
for(i=;i<=n1;i++)scanf("%d",&a[i]);
for(i=;i<=n2;i++)scanf("%d",&b[i]);
for(i=;i<=n1;i++)
for(j=;j<=n2;j++){
f[i][j]=max(f[i][j-],f[i-][j]);
if(a[i]==b[j])continue;
int k=i-;
while(k && a[k]!=b[j])k--;int pos1=k;
k=j-;
while(k && b[k]!=a[i])k--;int pos2=k;
if(pos1&&pos2) f[i][j]=max(f[i][j],f[pos1-][pos2-]+);
}
printf("%d\n",f[n1][n2]);
}
return ;
}

POJ1692 Crossed Matchings的更多相关文章

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

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

  2. POJ 1692 Crossed Matchings(DP)

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

  3. 【POJ】1692 Crossed Matchings

    经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...

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

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

  5. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  6. dp题目列表

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  7. poj 动态规划题目列表及总结

    此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...

  8. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

  9. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

随机推荐

  1. PAT 乙级 1024

    题目 题目地址:PAT 乙级 1024 题解 模拟题,重点需要考虑到各种不同情况:简单来说一下: 因为输入格式固定,所以把不同的部分分别存储和处理可以在很大程度上简化运算:其中需要考虑最多的就是小数部 ...

  2. tp5.1发送邮件

    <?php namespace app\admin\controller; use think\Controller; use think\Request; use PHPMailer\PHPM ...

  3. .net core IdentityServer4 使用query参数

    基本用法请参考官方文档:https://identityserver4.readthedocs.io/en/latest/index.html 这里不对具体用法进行说明,一般情况下,Startup添加 ...

  4. php下关于Cannot use a scalar value as an array的解决办法

    今天在测试php程序的时候,出现了一个错误提示:Cannot use a scalar value as an array,这个错误提示前几天也出过,当时好像稍微调了一下就好了,也没深究,今天却又出现 ...

  5. Redis之List类型操作

    接口: package com.net.test.redis.base.dao; import java.util.List; /** * @author *** * @Time:2017年8月10日 ...

  6. 数据分析处理库Pandas——索引

    显示DataFrame结构中的指定列 使用iloc索引 指定一行的信息 指定多行信息 备注:第[1,5)行信息. 指定行和列 备注:第[0,5)行中第[1,3)列信息. 使用loc索引 指定行信息 备 ...

  7. CMD终端关于pip报错,scrapy报错的一种处理方法

    CMD终端关于pip报错,scrapy报错的一种处理方法 如果在终端输入pip,或scrapy,报如下错误: Fatal error in launcher: Unable to create pro ...

  8. POJ 3320 尺取法(基础题)

    Jessica's Reading Problem Description Jessica's a very lovely girl wooed by lots of boys. Recently s ...

  9. 洛谷 P1736 创意吃鱼法(多维DP)

    题目描述 回到家中的猫猫把三桶鱼全部转移到了她那长方形大池子中,然后开始思考:到底要以何种方法吃鱼呢(猫猫就是这么可爱,吃鱼也要想好吃法 ^_*).她发现,把大池子视为01矩阵(0表示对应位置无鱼,1 ...

  10. HDU 1535 S-Nim(SG函数)

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...