zoj 1425 最大交叉匹配
Crossed Matchings
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 file 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 file 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
/*
zoj 1425 最大交叉匹配
题目大意:给两行序列,求他们的最大交叉数*2。
定义交叉:上下相同的数字连线,两条这样的线相交,
每个数字只能用与一条线,且两条线的数字不等。
定义dp(i,j)表示第一行至i,第二行至j,的最大交叉数,
dp(i,j)=max(dp(i-1,j-1)((i,j都不是))dp(i-1,j)(i不是),dp(i,j-1)(j不是),dp(n-1,m-1)+1(n-j,m-i交叉))。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int maxn=;
int dp[maxn][maxn];
int a[maxn],b[maxn]; inline int max(int a,int b){return a>b?a:b;} int main()
{
int t,i,j,k,l,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) scanf("%d",a+i);
for(i=;i<=m;i++) scanf("%d",b+i);
memset(dp,,sizeof(dp));
for(i=;i<=n;i++)
{
for(j=;j<=m;j++)
{
dp[i][j]=max(dp[i-][j-],max(dp[i-][j],dp[i][j-]));
if(a[i]==b[j]) continue;//两条交叉线段必须数字不同
k=i-;l=j-;
while(k> && a[k]!=b[j]) k--;
while(l> && a[i]!=b[l]) l--;
if(k && l) dp[i][j]=max(dp[k-][l-]+,dp[i][j]);
}
}
printf("%d\n",dp[n][m]*);
}
return ;
}
zoj 1425 最大交叉匹配的更多相关文章
- [ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- POJ 1274 The Perfect Stall || POJ 1469 COURSES(zoj 1140)二分图匹配
两题二分图匹配的题: 1.一个农民有n头牛和m个畜栏,对于每个畜栏,每头牛有不同喜好,有的想去,有的不想,对于给定的喜好表,你需要求出最大可以满足多少头牛的需求. 2.给你学生数和课程数,以及学生上的 ...
- zoj 3460 二分+二分图匹配
不错的思想 /* 大致题意: 用n个导弹发射塔攻击m个目标.每个发射架在某个时刻只能为 一颗导弹服务,发射一颗导弹需要准备t1的时间,一颗导弹从发 射到击中目标的时间与目标到发射架的距离有关.每颗导弹 ...
- 【转载】使用Pandas进行数据匹配
使用Pandas进行数据匹配 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas进行数据匹配 目录 merge()介绍 inner模式匹配 lefg模式匹配 right模式匹配 outer模式 ...
- zoj 3171 The Hidden 7's
这道题,我在网上看到两种dp,不过基本原理是一样的,不过感觉还是后面的一种比较巧妙!因为我对动态不是很熟,自能加上一些自己的理解,写上注释. 1) #include <stdio.h> # ...
- SALM入门笔记(1):特征点的匹配
SLAM 主要分为两个部分:前端和后端,前端也就是视觉里程计(VO),它根据相邻图像的信息粗略的估计出相机的运动,给后端提供较好的初始值.VO的实现方法可以根据是否需要提取特征分为两类:基于特征点的方 ...
- SLAM入门之视觉里程计(1):特征点的匹配
SLAM 主要分为两个部分:前端和后端,前端也就是视觉里程计(VO),它根据相邻图像的信息粗略的估计出相机的运动,给后端提供较好的初始值.VO的实现方法可以根据是否需要提取特征分为两类:基于特征点的方 ...
- 转载:使用Pandas进行数据匹配
使用Pandas进行数据匹配 本文转载自:蓝鲸的网站分析笔记 原文链接:使用Pandas进行数据匹配 目录 merge()介绍 inner模式匹配 lefg模式匹配 right模式匹配 outer模式 ...
- 一位学长的ACM总结(感触颇深)
发信人: fennec (fennec), 信区: Algorithm 标 题: acm 总结 by fennec 发信站: 吉林大学牡丹园站 (Wed Dec 8 16:27:55 2004) AC ...
随机推荐
- How to restrict root user to access or modify a file and directory in Linux
Now in this article I will show you steps to prevent or restrict access of root user to access certa ...
- 解读express框架
#解读Express 框架 1. package.json文件:express工程的配置文件 2. 为什么可以执行npm start?相当于执行 node ./bin/www "script ...
- shell脚本,awk利用NF来计算文本显示的行数。
解释: 1.awk 'NF{a++;print a,$0;next}1' file4 首先判断NF是否存在值,第一行第二行第三行第四行都存在,进行执行后面的输出,输出后碰到next后,就结束了后面的操 ...
- 通过luac编译lua脚本
在lua官网下载一个lua的release包,里面有已经编译好的lua启动文件(包括lua.exe),其中还有luac.exe, 这个程序是用来将lua文件编译成二进制码, 使用方法很简单,在控制台调 ...
- 51nod——2502最多分成多少块
数据范围好小... 题目中没说要升序降序,不过样例解释里可以看出是要升序. #include <bits/stdc++.h> using namespace std; ],b[],visi ...
- Linux - NodeJS安装
1> 去NodeJS官网 https://nodejs.org/en/ 或 中文网 http://nodejs.cn/download/ 拷贝相应版本的安装文件,如下图: 2> 执行 wg ...
- Docker系列一:Docker的介绍和安装
Docker介绍 Docker是指容器化技术,用于支持创建和实验Linux Container.借助Docker,你可以将容器当做重量轻.模块化的虚拟机来使用,同时,你还将获得高度的灵活性,从而实现对 ...
- 【Maven】 (请使用 -source 8 或更高版本以启用 lambda 表达式)
在使用mvn install编译maven项目时,报了 “ (请使用 -source 8 或更高版本以启用 lambda 表达式)”错误,是因为设置的maven默认jdk编译版本太低的问题. 可使用两 ...
- 进入JVM的世界:《深入理解JVM虚拟机》-- 思维导图
进入JVM的世界:<深入理解JVM虚拟机>-- 思维导图 之前一直都是零零散散的看了些JVM的知识,心想这样不行啊!于是便抽空看了一下这本神书,阅罢,醍醐灌顶.豁然开朗.真正的是知其然,更 ...
- 五一4天就背这些Python面试题了,Python面试题No12
第1题: Python 中的 os 模块常见方法? os 属于 python内置模块,所以细节在官网有详细的说明,本道面试题考察的是基础能力了,所以把你知道的都告诉面试官吧 官网地址 https:// ...