[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 ...
随机推荐
- 多线程之 Final变量 详解
原文: http://www.tuicool.com/articles/2Yjmqy 并发编程网:http://ifeve.com/java-memory-model/ 总结: Final 变量在并发 ...
- 博客的开端,找对象不再new
今天是第一次用blog,小白开始完善了!! 希望大家多多照顾一下.
- eclipse javascript验证报错
项目右键->properties
- javascript 的事件--阻止冒泡
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 02-JAVA中的基本语法
第一个动手动脑: 首先用枚举类型定义两个变量,然后判断两个变量是否引用同一对象,在判断枚举类型是否是原始数据类型,再从字符串中转换,最后列出所有的对象. 很明显,这两个变量都是枚举类型,但是这两个变量 ...
- Java学习笔记16--异常
异常 异常是导致程序中断运行的一种指令流,如果不对异常进行正确的处理,则可能导致程序的中断执行,造成不必要的损失, 所以在程序的设计中必须要考虑各种异常的发生,并正确的做好相应的处理,这样才能保证程序 ...
- MySQL表分区
MySQL的表分区 一.什么是表分区通俗地讲表分区是将一大表,根据条件分割成若干个小表.mysql5.1开始支持数据表分区了.如:某用户表的记录超过了600万条,那么就可以根据入库日期将表分区,也可以 ...
- error C2065: 'INVALID_SET_FILE_POINTER' : undeclared identifier
Searching MSDN for that constant brings up one result: it's a failure code for SetFilePointer() and ...
- xsltproc docbook 转 html
/etc/xml/catalog <?xml version="1.0" encoding="UTF-8"?> <catalog xmlns= ...
- DB2 UDB DBA 核对清单
本文摘自 http://www-128.ibm.com/developerworks/cn/db2/library/techarticles/dm-0404snow/index.htmlDB2 UDB ...