POJ 1692 Crossed Matchings(DP)
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 Sample Output 6 题意:同样数字能够连接可是必须和不同数字的连接交叉。问最大可能性 dp[i][j]表示第一行的前i个和第二行的前j个的最大可能。 |
#include<limits.h>
using namespace std;
int a[110],b[110];
int dp[110][110];
int n,m,t; int main()
{
int k1,k2;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int j=1;j<=m;j++)
scanf("%d",&b[j]);
memset(dp,0,sizeof(dp));
for(int i=2;i<=n;i++)
{
for(int j=2;j<=m;j++)
{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);//相等时可达到的dp[i][j]的状态的最大值
if(a[i]!=b[j])
{
for(k1=i;k1>=1;k1--)
{
if(b[j]==a[k1])
break;
}
for(k2=j;k2>=1;k2--)
{
if(a[i]==b[k2])
break;
}
if(k1&&k2)
dp[i][j]=max(dp[i][j],dp[k1-1][k2-1]+2);//更新dp[i][j]
}
}
}
printf("%d\n",dp[n][m]);
}
return 0;
}
POJ 1692 Crossed Matchings(DP)的更多相关文章
- POJ 1692 Crossed Matchings dp[][] 比较有意思的dp
http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 【POJ】1692 Crossed Matchings
经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...
- POJ 1260:Pearls(DP)
http://poj.org/problem?id=1260 Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8 ...
- POJ 2192 :Zipper(DP)
http://poj.org/problem?id=2192 Zipper Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1 ...
- POJ 3254 Corn Fields(DP + 状态压缩)
题目链接:http://poj.org/problem?id=3254 题目大意:Farmer John 放牧cow,有些草地上的草是不能吃的,用0表示,然后规定两头牛不能相邻放牧.问你有多少种放牧方 ...
- poj - 1191 - 棋盘切割(dp)
题意:将一个8*8的棋盘(每一个单元正方形有个分值)沿直线(竖或横)割掉一块,留下一块,对留下的这块继续这样操作,总共进行n - 1次,得到n块(1 < n < 15)矩形,每一个矩形的分 ...
- POJ 1160 Post Office(DP+经典预处理)
题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...
- POJ 1191 棋盘分割(DP)
题目链接 题意 : 中文题不详述. 思路 : 黑书上116页讲的很详细.不过你需要在之前预处理一下面积,那样的话之后列式子比较方便一些. 先把均方差那个公式变形, 另X表示x的平均值,两边平方得 平均 ...
随机推荐
- 2015.8.3 Java
今天继续学习Java 用的是eclipse IDE 这个ide怪怪的,但是有一个很方便的功能 就是通过右键选择source 可以点击Generate Getters and Setters生成属性的 ...
- HDU 4497 GCD and LCM(分解质因子+排列组合)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497 题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L.告诉你G.L,求满 ...
- BZOJ 1044: [HAOI2008]木棍分割(二分答案 + dp)
第一问可以二分答案,然后贪心来判断. 第二问dp, dp[i][j] = sigma(dp[k][j - 1]) (1 <= k <i, sum[i] - sum[k] <= ans ...
- JBoss 系列七十:一个简单的 CDI Web 应用
概述 本文通过一个简单的 CDI Web 应用演示dependency injection, scope, qualifiers 以及EL整合.应用部署完成后我们可以通过http://localhos ...
- linux基础命令整理
1 显示当前工作目录 pwd 2 查看目录下的所有内容 ls ls -l 以列方式查看 ls -a 显示所有 ...
- dedecms(织梦)自定义表单后台显示不全 自定义模型当中添加自定义字段后在后台添加内容后不显示解决方案
我们常用dedecms 自定义表单做留言功能.但是偶尔会遇到这样一个问题,就是 在前台提交表单后..后天显示不全.特别是中文字符 都不会显示, 比如下图: 这是因为 如果你织梦是gbk的话那就对了 ...
- MyEclipse 怎样手动编译整个项目
去掉自动编译的对勾,项目上就有build project,在菜单project->>build automatically. MyEclipse 菜单 Project->Build ...
- jqmobile
标准页面结构 <!DOCTYPE html> <html> <head> <title>Page Title</title> <lin ...
- BZOJ 1022 小约翰的游戏 (Anti-Nim游戏)
题解:注意题目中规定取到最后一粒石子的人算输,所以是Anti-Nim游戏,胜负判断为: 先手必胜: 1.所有堆的石子数都为1且游戏的SG值为0: 2.有些堆的石子数大于1且游戏的SG值不为0. #in ...
- 【浏览器那些基础】Android平台有那些CPU类型
在编译Android应用的时候,需要配置支持的CPU类型,而目前Android支持的CPU类型包含了ARM和X86,所以在编译前需要指定CPU类型(不同的cpu的特性不一样): X86系列的 expo ...