poj1692 Crossed Matchings

本文探讨了一个特殊的交叉匹配问题,即给定两行正整数,如何找到最多数量的匹配对,使得每对匹配的数字相同,并且每一对匹配恰好与其他一对不同数值的匹配相交叉。文章提供了详细的解题思路与实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Crossed Matchings
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3214 Accepted: 2012

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



题解:

题意真的是醉了,看都看不懂。

dalao们的题意:给出两行数,求上下匹配的最多组数是多少。匹配规则 1:匹配对的数字必须相同。2:每个匹配必须有且只能有一个匹配与之相交叉,且相交叉的两组匹配数字必须不同。 3:一个数最多只能匹配一次 。


我们显然可以发现交叉的两对数肯定是成对成对出现的,而且中间不可能有其他连线。于是我们枚举第一队交叉的数,然后暴力寻找另一对与它交叉的数。

可以设dp[i][j]表示匹配到i,j的最大匹配数。那么dp[i]][j]=max(dp[i-1][j],dp[i][j-1],dp[i][j](因为枚举的时候可能这个位置上的值已经被更新过了))。

如果a[ii]==b[jj]那么dp[max(i,ii)][max(j,jj)]=max(dp[min(i,ii)-1][min(j,jj)-1]+2,dp[max(i,ii)][max(j,jj)])(这样就保证了每次枚举的两对交叉线不会重合)


代码:

#include <iostream>  
#include <fstream>  
#include <cstdio>  
#include <cmath>  
#include <map>  
#include <set>  
#include <bitset>  
#include <ctime>  
#include <cstring>  
#include <algorithm>  
#include <stack>  
#include <queue>  
#include <vector>  
#include <list> 
using namespace std;
int t1,i,n,m,j,ii,jj,a[10001],b[10001],f[1001][1001],ans;
int main(){
	scanf("%d",&t1);
	while(t1--){
		memset(f,0,sizeof(f));
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		for(i=1;i<=m;i++)scanf("%d",&b[i]);
		for(i=1;i<=n;i++)
		 for(j=1;j<=m;j++){
		 	f[i][j]=max(f[i][j],max(f[i-1][j],f[i][j-1]));
		  if(a[i]==b[j]){
		  	for(ii=i-1;ii;ii--)
		  	 for(jj=j+1;jj<=m;jj++)
		  	  if(a[ii]==b[jj]&&a[ii]!=a[i]){
		  	  			  	//printf("%d %d %d %d\n",i,j,ii,jj);
		  	  	f[max(i,ii)][max(j,jj)]=max(f[max(i,ii)][max(j,jj)],f[min(i,ii)-1][min(j,jj)-1]+2);
				}
			for(ii=i+1;ii<=n;ii++)
			 for(jj=j-1;jj;jj--)
			 	if(a[ii]==b[jj]&&a[ii]!=a[i]){
			 		//printf("%d %d %d %d\n",i,j,ii,jj);
		  	  	f[max(i,ii)][max(j,jj)]=max(f[max(i,ii)][max(j,jj)],f[min(i,ii)-1][min(j,jj)-1]+2);
				}
		  }
}
	ans=0;
	for(i=1;i<=n;i++)
	 for(j=1;j<=m;j++)ans=max(ans,f[i][j]);
	 printf("%d\n",ans);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值