[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 ...
随机推荐
- UVM的factory机制
在UVM中使用工厂模式基本上分为三个步骤: 1. 注册 当定义一个类的时候,它的类型必须要注册,UVM已经提供了专用的宏. `uvm_component_utils(class_type_name) ...
- git常用操作
批量删除以bran开头的本地分支 git branch |grep 'bran'|xargs git branch -d
- 数据库数据怎样导出成Excle表格或Word文档?
数据导出:将数据库的数据导出成Excel工作表或Word文档 方法:将一个泛型集合导出出去 主要使用: SaveFileDialog StreamWriter 导出代码: private void b ...
- signtool.exe not found
When check the [sign the Xap File] checkbox, build project failed due to signtool.exe not found. Fol ...
- union all 里面的order by
例1: SELECT 1 order2 FROM dual union all SELECT 3 order2 FROM dual union all SELECT 2 order1 FROM dua ...
- java基础八 [序列化和文件的输入/输出](阅读Head First Java记录)
对象具有状态和行为两种属性.行为存在类中的方法中,想要保存状态有多种方法,这里介绍两种: 一是保存整个当前对象本身(通过序列化):一是将对象中各个状态值保存到文件中(这种方式可以给其他非JAVA程序用 ...
- 笔记本Linux推荐
1.CUB LINUX Cub Linux 是一个最好的选择.他的前身来自著名的 Chromium OS , Cub Linux 能够运行在各种各样的笔记本上面.即便是早年的老机,亦或是现在的新机.从 ...
- Pojo类(plain ordinary java object)
POJO有一些private的参数作为对象的属性.然后针对每个参数定义了get和set方法作为访问的接口.例如:public class User {private long id;private S ...
- PHP 输出缓冲控制(Output Control) 学习
php 缓冲简介 其实我对php ob 系列印象还是很模糊,具体怎么玩的,还不是很了解,平时curd,确实对这些内容没有深入.作为phper 甚是惭愧.网上搜了一通,互相copy,代码运行不能出现作者 ...
- option3
option = { tooltip : { trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)" }, le ...