poj 1692(动态规划)
2016-04-11 16:19
330 查看
Crossed Matchings
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
Sample Output
Source
Tehran 1999
这题借鉴了别人的想法。http://blog.csdn.net/gubojun123/article/details/8447071
开始觉得是二分图匹配,然后现在刚弄到动归。。二分图也忘了,被题意吓唬住了。还是要多加练习啊。。有点区间DP的意思吧。。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2711 | Accepted: 1759 |
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
Tehran 1999
这题借鉴了别人的想法。http://blog.csdn.net/gubojun123/article/details/8447071
开始觉得是二分图匹配,然后现在刚弄到动归。。二分图也忘了,被题意吓唬住了。还是要多加练习啊。。有点区间DP的意思吧。。
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; const int N = 105; int dp ; ///dp[i][j]表示第1行前i个字符和第二行前j个字符的最大匹配 int main() { int tcase; int a ,b ; scanf("%d",&tcase); while(tcase--){ int n1,n2; scanf("%d%d",&n1,&n2); for(int i=1;i<=n1;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n2;i++){ scanf("%d",&b[i]); } memset(dp,0,sizeof(dp)); for(int i=1;i<=n1;i++){ for(int j=1;j<=n2;j++){ dp[i][j] = max(dp[i-1][j],dp[i][j-1]); if(a[i]!=b[j]){ int k1,k2; for(k1 = i-1;k1>0;k1--){ if(a[k1]==b[j]) break; } for(k2=j-1;k2>0;k2--){ if(b[k2]==a[i]) break; } if(k1!=0&&k2!=0){ dp[i][j] = max(dp[i][j],dp[k1-1][k2-1]+2); ///在 dp[k1-1][k2-1]之后又产生了两组新的匹配 } } } } printf("%d\n",dp[n1][n2]); } return 0; }
相关文章推荐
- hive udtf使用及explode
- 使用HandleErrorAttribute处理异常
- BZOJ4184: shallot
- Spring Boot 新一代Spring Java应用 : 你能感觉在使用 JavaEE 规范吗?
- macOS安装putty
- Android动态换肤(二、apk免安装插件方式)
- [置顶] 编写两个函数,分别求两个整数的最大公约数和最小公倍数
- Android动态换肤(二、apk免安装插件方式)
- Apache apr中内存分配子的current_free_index成员作用 转转转
- 响铃:企业SaaS上演“三国”杀,谁才是决定生死的变量?
- android apk应用程序如何用代码实现 自己安装自己(自升级)?
- 15款最好的 jQuery Modal(模态窗口)插件
- 【原】Project configuration is not up-to-date with pom.xml
- 青蛙跳100级台阶算法,完整可运行,php版本
- 礼拜一log~flexslider埋坑指南 firefox火狐浏览器不支持zoom的解决办法
- SpringMVC+Spring+Mybatis+Mysql项目搭建
- 详解Python的Django框架中manage命令的使用与扩展
- Android自定义下拉刷新动画--仿百度外卖下拉刷新
- 21天学通Linux嵌入式开发教程
- ZOJ 3780 - Paint the Grid Again