POJ1692 Crossed Matchings
Time Limit: 1000MS
| Memory Limit: 10000K | ||
| Total Submissions: 2738 | Accepted: 1777 |
Description

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
first line of the input 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
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
Source
两个交叉的匹配为一组,每找到一组可行的匹配,答案数+2 。
设:f[上方匹配位置][下方匹配位置]=最优解
假设现在扫到了上方数组的i点和下方数组的j点。首先可以想到如果没有新的匹配,f[i][j]=max(f[i][j-1],f[i-1][j])
接着考虑新的匹配,在上方数组中从i往前找,找到最近的pos1使a[pos1]=b[j],同理在下方找到b[pos2]=a[i],那么pos1-j,pos2-i两条连线必然交叉,得到动归方程:
f[i][j]=max(f[i][j],f[pos1-1][pos2-1]+2)
/**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int mxn=;
int n1,n2;
int a[mxn],b[mxn];
int f[mxn][mxn];
int main(){
int T;
scanf("%d",&T);
int i,j;
while(T--){
memset(f,,sizeof f);
scanf("%d%d",&n1,&n2);
for(i=;i<=n1;i++)scanf("%d",&a[i]);
for(i=;i<=n2;i++)scanf("%d",&b[i]);
for(i=;i<=n1;i++)
for(j=;j<=n2;j++){
f[i][j]=max(f[i][j-],f[i-][j]);
if(a[i]==b[j])continue;
int k=i-;
while(k && a[k]!=b[j])k--;int pos1=k;
k=j-;
while(k && b[k]!=a[i])k--;int pos2=k;
if(pos1&&pos2) f[i][j]=max(f[i][j],f[pos1-][pos2-]+);
}
printf("%d\n",f[n1][n2]);
}
return ;
}
POJ1692 Crossed Matchings的更多相关文章
- [ACM_动态规划] ZOJ 1425 Crossed Matchings(交叉最大匹配 动态规划)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- POJ 1692 Crossed Matchings(DP)
Description There are two rows of positive integer numbers. We can draw one line segment between any ...
- 【POJ】1692 Crossed Matchings
经典DP,想了很久,开始想复杂了. #include <iostream> using namespace std; #define MAXNUM 100 int mymax(int a, ...
- POJ 1692 Crossed Matchings dp[][] 比较有意思的dp
http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...
- 别人整理的DP大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
- dp题目列表
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- poj 动态规划题目列表及总结
此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 11 ...
- [转] POJ DP问题
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
- poj动态规划列表
[1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...
随机推荐
- C++ 无限定名称查找
无限定名称查找 (关键字:懒惰,挑捡,using指令的特殊性) 无限定名称查找实际上就是指没有限定(名称空间和名称空间运算符)名存在的一个名字的出现,其中对于using指令,其内部包含的所有的声明是被 ...
- 二、MySQL 管理
MySQL 管理 启动及关闭 MySQL 服务器 Windows 系统下 在 Windows 系统下,打开命令窗口(cmd),进入 MySQL 安装目录的 bin 目录. 启动: cd c:/mysq ...
- LeetCode#453 最小移动次数使数组元素相等
给定一个长度为 n 的非空整数数组,找到让数组所有元素相等的最小移动次数.每次移动可以使 n - 1 个元素增加 1. 示例: 输入: [,,] 输出: 解释: 只需要3次移动(注意每次移动会增加两个 ...
- Zookeeper协调服务系统·ELK日志管理系统简介
Zookeeper协调服务系统: 说明:它分布式系统中的协调服务系统,是Hadoop下的一个子项目,可提供的服务有:名字服务.配置服务.分布式同步.组服务等. 3个角色:Leaders.Follow. ...
- Python linecache模块
Table of Contents 1. linecache 1.1. 其它 2. 参考资料 linecache 今天分享一个python的小模块: linecache, 可以用它方便地获取某一文件某 ...
- [2016-04-19 15:46:03 - IceHoloReader1.0] Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER [20
[2016-04-19 15:46:03 - IceHoloReader1.0] Installation error: INSTALL_FAILED_CONFLICTING_PROVIDER [20 ...
- MySQL基础9-主键约束、外键约束、等值连接查询、一对一和多对多关系
1.主键约束和外键约束 外键约束 * 外键必须是另一表的主键的值(外键要引用主键!) * 外键可以重复 * 外键可以为空 * 一张表中可以有多个外键! 概念模型在数据库中成为表 数据库表中的多对一关系 ...
- mongoTemplate聚合操作Demo
package com.tangzhe.mongodb.mongotemplate; import com.mongodb.BasicDBObject; import com.mongodb.DBOb ...
- Flask 中蓝图的两种表现形式
最近在学Flask,特有的@X.route 很适合RESTfuld API, 一般小型应用,在一个py文件中就可以完成,但是维护起来比较麻烦. 想体验Django那样的MVT模式, 看到 Flask提 ...
- Spring进阶-怎样集成定时调度Quartz
在一些项目里面比如进销存系统,对一些过期图片的定时清理或者库存不足发出预警提示,就需要用到定时调度技术. 每当经过一段时间,程序会自动执行,就是定时调度.如果要使用定时调度,则必须保证程序始终运行才行 ...