POJ1692 Crossed Matchings(dp)
来源:互联网 发布:蓝科型材优化破解版 编辑:程序博客网 时间:2024/04/24 23:40
Crossed Matchings
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 2938 Accepted: 1910
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.
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
36 61 3 1 3 1 33 1 3 1 3 14 41 1 3 3 1 1 3 3 12 111 2 3 3 2 4 1 5 1 3 5 103 1 2 3 2 4 12 1 5 5 3
Sample Output
608
Source
Tehran 1999
思路:dp[i][j]:=第一行前i个数与第二行前j个数的最大匹配数;
状态转移方程:
1)当a[i]==b[j]时,显然dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
2)当a[i]!=b[j]时,假设a[i]和b[q]相匹配(q<j),b[j]和a[p]相匹配(p<i),则显然dp[i][j]=max{dp[p-1][q-1]+2}
根据状态转移方程观察一下时间复杂度,在 2)中显然需要4个for循环,时间复杂度为O(m^2*n^2),显然时间过大,需要优化。
怎么优化 2)呢?显然我们选择相匹配的肯定是在满足相匹配时最大的位置q和p,我们可以通过预处理找出最大的位置q和p,设定两个表示位置的数组q[maxn][maxn],p[maxn][maxn],其中q[i][j]表示在数组b中下标小于j且与a[i]相匹配的的数的最大位置,而p[j][i]表示在数组a中下标小于i且与b[j]相匹配的数的最大位置;
经过优化显然时间复杂度为O(m*n);
AC代码:
#include <bits/stdc++.h>//提交poj把头文件改一下using namespace std;const int maxn=1001;int dp[maxn][maxn],p[maxn][maxn],q[maxn][maxn];int a[maxn],b[maxn];int m,n,T;int main(){ scanf("%d",&T);while (T--) {cin>>m>>n;for(int i=1;i<=m;i++) cin>>a[i];for(int j=1;j<=n;j++) cin>>b[j]; for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) { if(a[i]==b[j-1]) q[i][j]=j-1; else q[i][j]=q[i][j-1];}for(int j=1;j<=n;j++) for(int i=1;i<=m;i++) { if(b[j]==a[i-1]) p[j][i]=i-1; else p[j][i]=p[j][i-1];} for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) { dp[i][j]=max(dp[i][j-1],dp[i-1][j]); if(a[i]!=b[j]&&p[j][i]>0&&q[i][j]>0)//显然应该还要满足位置>0 dp[i][j]=max(dp[i][j],dp[p[j][i]-1][q[i][j]-1]+2); }cout<<dp[m][n]<<endl;}return 0;}
阅读全文
1 0
- POJ1692 Crossed Matchings(dp)
- POJ1692 Crossed Matchings DP
- ZOJ1425 POJ1692 Crossed Matchings
- POJ1692:Crossed Matchings
- POJ1692 Crossed Matchings
- 北大ACM poj1692 Crossed Matchings(动态规划)
- poj1692 Crossed Matchings(dp,最长公共子序列变形,好题)
- POJ 1692 Crossed Matchings(DP)
- ZOJ1425 Crossed Matchings dp
- Crossed Matchings zoj1425 dp
- POJ1692————Crossed Matchings(动态规划)
- POJ 1692 Crossed Matchings(dp).
- POJ 1692 Crossed Matchings(DP)
- POJ 1692 Crossed Matchings(DP)
- POJ 1692 Crossed Matchings(DP)
- poj 1692 Crossed Matchings DP
- poj 1692 Crossed Matchings(DP)
- POJ 1692 Crossed Matchings (DP) #by Plato
- 顺序表,头结点链表,头指针链表,头结点循环链表
- 快速读入int型整数模板
- windows service 安装/卸载
- android:screenOrientation的说明
- Java运算符
- POJ1692 Crossed Matchings(dp)
- Project facet Java version 1.8 is not supported.
- Js的关键字
- XListView实现上拉刷新下拉加载
- Data Science Bowl 2017数据预处理
- PB笔记--语法
- 从移动端click到摇一摇
- Java设计模式七大原则
- linux下实时查看tomcat运行日志