POJ1692:Crossed Matchings
来源:互联网 发布:哪家证券公司好些 知乎 编辑:程序博客网 时间:2024/04/26 19:31
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.
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
Tehran 1999
引用下别人的解释吧,自认不能比他写的更详细了。。。
题意:给出两行数,求上下匹配的最多组数是多少。
匹配规则
1,匹配对的数字必须相同
2.每个匹配必须有且只能有一个匹配与之相交叉,且相交叉的两组匹配数字必须不同
2,一个数最多只能匹配一次
匹配规则
1,匹配对的数字必须相同
2.每个匹配必须有且只能有一个匹配与之相交叉,且相交叉的两组匹配数字必须不同
2,一个数最多只能匹配一次
分析:用dp[i][j]表示第一行取i个数,第二行取j个数字的最多匹配项
对于某个dp[i][j]:
1.不匹配第一行i个,或不匹配第二行第j个:dp[i][j]=Max(dp[i-1][j],dp[i][j-1])
2.如果a[i]==b[j],不产生新匹配,匹配结果为1的值
3.若a[i]!=b[j]:
a.则第一行从i往前扫,直到扫到第一个a[k1]==b[j](k1 b.同理,第二行从j往前扫,直到扫到第一个b[k2]==a[i](k2 若找不到这样的k1,k2则不能才产生新匹配,跳过
若存在这样的k1,k2,此时匹配(a[i],b[k2])、(a[k1],b[j])匹配,
才生新的匹配情形,匹配数量为:dp[k1-1][k2-1]+2.
最优dp[i][j]=Max(1,2,3);
#include <iostream>#include <stdio.h>#include <string.h>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <algorithm>#include <climits>using namespace std;#define ls 2*i#define rs 2*i+1#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,x) memset(a,x,sizeof(a))#define w(a) while(a)#define LL long longconst double pi = acos(-1.0);#define N 505#define mod 19999997#define INF 0x3f3f3f3f#define exp 1e-8int t,n,m,a[105],b[105],dp[105][105];int main(){ int i,j,k; scanf("%d",&t); w(t--) { scanf("%d%d",&n,&m); up(i,1,n) scanf("%d",&a[i]); up(i,1,m) scanf("%d",&b[i]); mem(dp,0); up(i,2,n) { up(j,2,m) { dp[i][j]=max(dp[i-1][j],dp[i][j-1]); if(a[i]!=b[j]) { int k1=0,k2=0; down(k1,i-1,1) if(a[k1]==b[j]) break; down(k2,j-1,1) if(b[k2]==a[i]) break; if(k1&&k2) dp[i][j]=max(dp[i][j],dp[k1-1][k2-1]+2); } } } printf("%d\n",dp[n][m]); } return 0;}
0 0
- ZOJ1425 POJ1692 Crossed Matchings
- POJ1692:Crossed Matchings
- POJ1692 Crossed Matchings
- POJ1692 Crossed Matchings DP
- POJ1692 Crossed Matchings(dp)
- 北大ACM poj1692 Crossed Matchings(动态规划)
- POJ1692————Crossed Matchings(动态规划)
- poj1692 Crossed Matchings(dp,最长公共子序列变形,好题)
- PKU1692 Crossed Matchings
- POJ 1692 Crossed Matchings
- pku1692 Crossed Matchings
- poj 1692 Crossed Matchings
- 【动态规划】Crossed Matchings
- ZOJ1425 Crossed Matchings dp
- zoj 1425 Crossed Matchings
- Crossed Matchings zoj1425 dp
- POJ 1692 Crossed Matchings
- POJ 1692 Crossed Matchings
- Linux 技巧:让进程在后台可靠运行的几种方法
- ADB server didn't ACK * failed to start daemon * 和 Android Studio绝对实用的新手教程(1)导入第三方包或者运行eclipse项目
- 基数排序的C实现
- 数据库原理学习笔记——引言
- 模拟新浪微博随便看看栏目
- POJ1692:Crossed Matchings
- 图结构练习——最小生成树
- 爱情让人智商变低
- 在windows2012 R2下安装oracle 11.2.0.4的GI时遇到的问题--bug导致
- 结构体内存偏移
- 一步一步跟我学习lucene(6)---lucene索引优化之多线程创建索引
- 利用cygwin编译cholmod以获得在windows上可用的库lib
- ubuntu查看端口命令
- 编译安装Xen 4.5(适用于CentOS6 Redhat6)