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的平均值,两边平方得 平均 ...
随机推荐
- asp.net 连接access数据库方法
在 Web.Config 中配置 Access 数据库驱动和数据库文件名称. 请看代码 <appSettings> <add key="DBDriver" val ...
- java内存映射文件
内存映射文件能够让我们创建和修改大文件(大到内存无法读入得文件),对于内存映射文件,我们可以认为是文件已经全部被读入到内存当中,然后当成一个大的数字来访问,简化修改文件的代码. 1.directBuf ...
- mysql 存储过程需要DELIMITER
DELIMITER &&CREATE PROCEDURE syncAdvertiser() BEGIN DECLARE id bigint; DECLARE _cur CURSOR F ...
- CSS 文章段落样式
#adiv p { text-align: left; text-indent: 2em; line-height:25px; font-family:微软雅黑; font-size:medium; ...
- C语言实现约瑟夫环讨论
[问题描述] 约瑟夫(Joseph)问题的一种描述是:编号为1,2,…,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数).一开始任选一个正整数作为报数上限值m,从第一个人开始按顺时针 ...
- css为网页顶部和底部都加入背景图
网页背景图是我们常用的功能,一般来说.给网页加一个背景图,只要在网页的body标签中加入css属性就行. 代码如下:<body style="background-image:url( ...
- 一个帝国cms [!--show.listpage--] css样式
1.在分页位置加<div class="pagepage">[!--show.listpage--]</div> </div>这个标签 2.在需 ...
- 在IT公司,project manager 基本上和秘书,助理什么的差不多
我感觉非常有道理,所以我不做Leader,哈哈哈
- [uva 11762]Race to 1[概率DP]
引用自:http://hi.baidu.com/aekdycoin/item/be20a91bb6cc3213e3f986d3,有改动 题意: 已知D, 每次从[1,D] 内的所有素数中选择一个Ni, ...
- js静态方法和实例方法
js静态方法 function foo(){} // 声明类 foo.method = function(){} // 方法体 使用:foo.method() js实例方法 function foo( ...

