[ACM_动态规划] ZOJ 1425 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
Output
Sample Input
Sample Output
题目大意:上下2排数据,找一个满足条件的最大匹配数(条件是任意一个匹配的连线都要被至少另一个不一样的匹配穿过)!
解题思路:opt[i][j]为 up[] 数组前 i 个数与 down[] 数组前 j 个数的最大匹配.递推关系:
opt[i][j] = max{ opt[i-1][j], opt[i][j-1], opt[a-1][b-1] + 2}
>_< :上式 a,b 的取值须满足 (1 <= a < i) && (1 <= b < j) 并且存在匹配 (up[a] == down[j]) && (down[b] == up[i]) && (up[a] != up[i])
#include<iostream>
#include<string.h>
using namespace std;
int M;
int N1,N2;
int up[],down[];
int opt[][];
int main(){
cin>>M;
while(M--){
cin>>N1>>N2;
memset(opt,,sizeof(opt));
for(int i=;i<=N1;i++)cin>>up[i];
for(int j=;j<=N2;j++)cin>>down[j]; for(int i=;i<=N1;i++){
for(int j=;j<=N2;j++){
opt[i][j]= opt[i-][j]>opt[i][j-] ? opt[i-][j]:opt[i][j-];
if(up[i]!=down[j]){//只有最后2个不一样时才有可能都和前面的有匹配
int t=;
for(int a=;a<i;a++){
for(int b=;b<j;b++){//遍历查找满足条件的t
if(up[a]==down[j] && up[i]==down[b] && t<opt[a-][b-]+)
t=opt[a-][b-]+;
}
}
opt[i][j]=opt[i][j]>t ? opt[i][j]:t;
}
}
} cout<<opt[N1][N2]<<'\n';
}return ;
}
[ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)的更多相关文章
- zoj 1425 最大交叉匹配
Crossed Matchings Time Limit: 2 Seconds Memory Limit: 65536 KB There are two rows of positive i ...
- sicily 1176. Two Ends (Top-down 动态规划+记忆化搜索 v.s. Bottom-up 动态规划)
Description In the two-player game "Two Ends", an even number of cards is laid out in a ro ...
- POJ 1692 Crossed Matchings(DP)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- POJ1692 Crossed Matchings
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2738 Accepted: 1777 Description The ...
- POJ 1692 Crossed Matchings dp[][] 比较有意思的dp
http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...
- ZOJ 1364 Machine Schedule(二分图最大匹配)
题意 机器调度问题 有两个机器A,B A有n种工作模式0...n-1 B有m种工作模式0...m-1 然后又k个任务要做 每一个任务能够用A机器的模式i或b机器的模式j来完毕 机器開始都处于模式0 每 ...
- ZOJ 3316 Game 一般图最大匹配带花树
一般图最大匹配带花树: 建图后,计算最大匹配数. 假设有一个联通块不是完美匹配,先手就能够走那个没被匹配到的点.后手不论怎么走,都必定走到一个被匹配的点上.先手就能够顺着这个交错路走下去,最后一定是后 ...
- [ACM_模拟] ZOJ 3713 [In 7-bit 特殊输出规则 7bits 16进制]
Very often, especially in programming contests, we treat a sequence of non-whitespace characters as ...
- [ACM_图论] ZOJ 3708 [Density of Power Network 线路密度,a->b=b->a去重]
The vast power system is the most complicated man-made system and the greatest engineering innovatio ...
随机推荐
- {Reship}{Matting}Image Matting
======================================== http://www.alphamatting.com/index.html ==================== ...
- bind的用处
刚做的项目,遇到过这样的问题,就是在动态追加标签时,给追加的标签添加事件时,在标签内追加不了,后来使用了delegate代理,能响应了,但也是不能给动态追加的代理 $("body" ...
- js中==与===的区别
- jquery.fileupload插件的简易使用日志
来源:http://yixiandave.iteye.com/blog/1897330 本文内容主要包含fileupload插件传递参数和取回服务器返回数据的方法 jquery.fileupload官 ...
- iframe自动适应高度
iframe代码,注意要写ID <iframe src="test.html" id="main" width="700" heigh ...
- webstorm下搭建编译less环境
webstorm自带less,不过要编译的话需要nodejs环境. 首先去node的主页下载对应版本的nodejs然后安装,下载地址:http://nodejs.org/ 安装完之后打开命令提示符(w ...
- WebView加载本地Html文件并实现点击效果
Webview是用来与前端交互的纽,可以加载本地Html文件,和网页并实现交互的功能. WebView通过WebSetting可以使用Android原生的JavascriptInterface来进行j ...
- Revenge of Nim hdu 4994 (博弈)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4994 题意:现在有两个人在取石子,共有n堆石子,每堆石子取完后才可以取下一堆石子,最后一个取石子的人 ...
- 【随笔】mOnOwall添加端口映射
mOnOwall是一个完整的嵌入防火墙软件包,当与一台嵌入个人电脑一起使用时,在免费使用自由软件的基础上,提供具备商业防火墙所有重要特性(包括易用). 这里通过配置mOnOwall的端口设置映射功能, ...
- 問題排查:F5啟動偵錯後所提示的錯誤 (1)
原始專案版本:Visual Studio 2005 開發環境:Visual Studio 2013 偵錯運行環境:IIS Express 啟動偵錯後,錯誤提示內容如下: HTTP 错误 500.23 ...