Crossed Matchings
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2711   Accepted: 1759

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

The
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

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

 
开始觉得是二分图匹配,然后现在刚弄到动归。。二分图也忘了,被题意吓唬住了。还是要多加练习啊。。有点区间DP的意思吧。。
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int N = ; int dp[N][N]; ///dp[i][j]表示第1行前i个字符和第二行前j个字符的最大匹配
int main()
{
int tcase;
int a[N],b[N];
scanf("%d",&tcase);
while(tcase--){
int n1,n2;
scanf("%d%d",&n1,&n2);
for(int i=;i<=n1;i++) {
scanf("%d",&a[i]);
}
for(int i=;i<=n2;i++){
scanf("%d",&b[i]);
}
memset(dp,,sizeof(dp));
for(int i=;i<=n1;i++){
for(int j=;j<=n2;j++){
dp[i][j] = max(dp[i-][j],dp[i][j-]);
if(a[i]!=b[j]){
int k1,k2;
for(k1 = i-;k1>;k1--){
if(a[k1]==b[j]) break;
}
for(k2=j-;k2>;k2--){
if(b[k2]==a[i]) break;
}
if(k1!=&&k2!=){
dp[i][j] = max(dp[i][j],dp[k1-][k2-]+); ///在 dp[k1-1][k2-1]之后又产生了两组新的匹配
}
}
}
}
printf("%d\n",dp[n1][n2]);
}
return ;
}

poj 1692(动态规划)的更多相关文章

  1. POJ 1692 Crossed Matchings dp[][] 比较有意思的dp

    http://poj.org/problem?id=1692 这题看完题后就觉得我肯定不会的了,但是题解却很好理解.- - ,做题阴影吗 所以我还是需要多思考. 题目是给定两个数组,要求找出最大匹配数 ...

  2. nyoj 17-单调递增最长子序列 && poj 2533(动态规划,演算法)

    17-单调递增最长子序列 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:21 submit:49 题目描述: 求一个字符串的最长递增子序列的长度 如 ...

  3. poj 3034 动态规划

    思路:这是一道坑爹的动态规划,思路很容易想到,就是细节. 用dp[t][i][j],表示在第t时间,锤子停在(i,j)位置能获得的最大数量.那么只要找到一个点转移到(i,j)收益最大即可. #incl ...

  4. poj 2498 动态规划

    思路:简单动态规划 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...

  5. poj 2287 动态规划

    用贪心简单证明之后就是一个从两头取的动态规划 #include <iostream> #include <cstring> #include <cstdio> #i ...

  6. POJ 2533 动态规划入门 (LIS)

    Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42914 Accepte ...

  7. poj 1821 动态规划

    思路:每次枚举每个工人的右边界j,维护最优的左边界k.那么dp[j]=max(dp[j],dp[k]+(j-k)*w[i].p): 对于每个工人的初值k=w[i].s-1; 令x=j-w[i].l,如 ...

  8. poj 1390 动态规划

    思路: 黑书的例题 #include<iostream> #include<cstring> #include<algorithm> #include<cma ...

  9. poj 1695 动态规划

    思路:和黑书上的跳舞机类似 #include<map> #include<set> #include<cmath> #include<queue> #i ...

随机推荐

  1. Ark组件[转]

    Ark组件简介 Ark组件是基于.NET 4.0框架开发的基础组件,封装了一些常用的功能方法,并提供了若干程序开发的基础框架. HttpSession简介 HttpSession是Ark组件中负责HT ...

  2. 关于Mybatis的@Param注解 及 mybatis Mapper中各种传递参数的方法

    原文:https://blog.csdn.net/mrqiang9001/article/details/79520436 关于Mybatis的@Param注解   Mybatis 作为一个轻量级的数 ...

  3. 解决requests获取源代码时中文乱码问题

    用requests获取源代码时,如果是中文网页,就可能会出现乱码,下面我以中关村的网站为例: import requests url = 'http://desk.zol.com.cn/meinv/' ...

  4. DOM动态增加控件

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <t ...

  5. 利用pdfJS实现以读取文件流方式在线展示pdf文件

    第一步:下载源码https://github.com/mozilla/pdf.js 第二步:构建PDF.js 第三步:修改viewer.js var DEFAULT_URL = 'compressed ...

  6. Spring @Async开启异步任务

    1. 开启异步 @SpringBootApplication @EnableAsync //开启异步任务 public class Application { @Bean(name="pro ...

  7. 动态规划:棋盘DP

    棋盘型动态规划在二维平面上进行操作.根据当前状态的可能情况做出一个最优的判断,或是依赖当前状态拓展出新的状态,在拓展的过程中,依赖的可能是上一层的最优值也可能是上一层的全部值. 这应该是最容易理解的一 ...

  8. truncate与delete以及drop

    truncate:删除整个表,但不删除定义(删除了整个表的数据,但表结构还在) drop:删除整个表,表数据和表结构都删除了 delete:删除表中数据 比较delete和drop 1.truncat ...

  9. 【BZOJ2693】jzptab [莫比乌斯反演]

    jzptab Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description   求 Input 第一行一个 ...

  10. 【BZOJ】1776: [Usaco2010 Hol]cowpol 奶牛政坛

    [题意]给定n个点的树,每个点属于一个分类,求每个分类中(至少有2个点)最远的两点距离.n<=200000 [算法]LCA [题解]结论:树上任意点集中最远的两点一定包含点集中深度最大的点(求树 ...