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 ...
随机推荐
- 02_5if switch分支与循环语句
02_5if switch分支与循环语句 1.语句 1.1条件语句-根据不同条件,执行不同语句. if if ... else if ... else if if ... else if ... el ...
- JS与 JSON(一个菜鸟的不正经日常)
今天学习了json的一些知识, 1 . 什么是json 1.1 JSON 英文全称 JavaScript Object Notation. 1.2 JSON 是一种轻量级的数据交换格式,用于存储和 ...
- CPL学习笔记(二)
数组 数组(array) 声明通用格式为: typeName arrayName[arraySize]. arrayName指定元素数目,必须为整型常量或const值.(不能是变量) int a[3] ...
- destoon添加修改会员信息时,信息丢失
最近做一destoon项目,因注册字段太多,分了几个步骤.分几个页面来修改公司信息.发现有时候修改时以前保存的字段莫名丢失.. 经查是 因为member.class.php add 和 edit时, ...
- python之函数基础总结
定义:函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. def sayhi(name): print("Hello, %s, I', nobo ...
- 在spring boot中使用webSocket组件(一)
最近在项目中使用到了spring的webSocket组件,在这里和大家分享下,如有错误,欢迎大家指正. 在这里我使用的IDE工具是Intellij idea,框架是spring boot.spring ...
- jmeter中重要组件及其执行顺序
jmeter中重要组件有:Sampler,计时器,前置处理器和后置处理器,断言,Controller,Listener和配置原件. 同类组件之间是从上到下的顺序执行,不同组件之间是按照以下的顺序执行的 ...
- Markdown 使用锚点
首先是建立一个跳转的连接: [说明文字](#jump) 然后标记要跳转到什么位置即可: <span id = "jump">跳转到这里:</span>
- 20,序列化模块 json,pickle,shelve
序列化模块 什么叫序列化? 将原本的字典,列表等内容转换成一个字符串的过程叫做序列化. 序列化的目的? 数据结构 通过序列化 转成 str. str 通过反序列化 转化成数据结构. json: jso ...
- selenium - 常用等待操作
# 4. 等待操作 # 强制等待from time import sleepsleep(10) # 隐性等待# 设置最长等待时间,在这个时间在只要有个时间点加载完成,则执行下一步代码,比sleep智能 ...