hdu-5495 LCS(置换)

题目链接:

LCS

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 818    Accepted Submission(s): 453

Problem Description

You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are permutation of {1,2,...,n}. You are going to find another permutation {p1,p2,...,pn} such that the length of LCS (longest common subsequence) of {ap1,ap2,...,apn} and {bp1,bp2,...,bpn} is maximum.

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n(1≤n≤105) - the length of the permutation. The second line contains n integers a1,a2,...,an. The third line contains nintegers b1,b2,...,bn.

The sum of n in the test cases will not exceed 2×106.

Output

For each test case, output the maximum length of LCS.

Sample Input

2
3
1 2 3
3 2 1
6
1 5 3 2 6 4
3 6 2 4 5 1

Sample Output

2
4

题意:

问把数列重新排一下的LCS的长度是多少;

思路:

可以发现把置换分成循环后除长度为一的循环外,每个循环都可以变换最后形成l-1的LCS,所以就好了;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,a[maxn],b[maxn],pos[maxn],vis[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]),pos[a[i]]=i,vis[i]=0;
        for(int i=1;i<=n;i++)scanf("%d",&b[i]);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[b[i]])
            {
                vis[b[i]]=1;
                int len=0,fa=b[i],p;
                while(1)
                {
                    p=pos[fa];
                    fa=b[p];
                    len++;
                    if(vis[fa])break;
                    vis[fa]=1;
                }
                if(len==1)ans++;
                else ans+=len-1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-10-01 21:46:31

hdu-5495 LCS(置换)的相关文章

hdu 5495 LCS 水题

LCS Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5495 Description 你有两个序列\{a_1,a_2,...,a_n\}{a?1??,a?2??,...,a?n??}和\{b_1,b_2,...,b_n\}{b?1??,b?2??,...,b?n??}. 他们都是11到nn的一个排列. 你需要找到另一个排列\{p_1,p_2,...,p_n\}{p?1?

hdu 5495 LCS(并查集)

Problem Description You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are permutation of {1,2,...,n}. You are going to find another permutation {p1,p2,...,pn} such that the length of LCS (longest common subsequence) of {ap1

bestcoder#58(div2) 1002 LCS 置换

bestcoder#58(div2)  1002 LCS    置换 LCS Accepts: 127 Submissions: 397 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 你有两个序列\{a_1,a_2,...,a_n\}{a?1??,a?2??,...,a?n??}和\{b_1,b_2,...,b_n\}{b?1??,b?2??,...,b?n??}. 他们

hdu 1503 LCS输出路径【dp】

hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左走.我用1表示向上走,2向左上方走,3向左走. 刚开始输入字符串时有两种方法,直接输入:或从地址后一位输入,即此时数组起始编号为1.直接输入时,dp[i][j]表示的是以s1[i-1],s2[j-1]为结尾LCS,另一种则就是表示以s1[i],s2[j]为结尾的LCS.两者在路径输出时有些差别,以前

hdu 1423(LCS+LIS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 好坑啊..还有公共串为0时的特殊判断,还有格式错误..看Discuss看知道除了最后一组测试数据之外都需要空行.. 其余的会LCS打印路径就行了. ///公共最长上升子序列 #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <

hdu 1080(LCS变形)

Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3008    Accepted Submission(s): 1701 Problem Description It is well known that a human gene can be considered as a sequence,

Advanced Fruits(HDU 1503 LCS变形)

Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2358    Accepted Submission(s): 1201Special Judge Problem Description The company "21st Century Fruits" has specialized in cr

hdu 1243(LCS变形)

反恐训练营 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4064    Accepted Submission(s): 967 Problem Description 当 今国际反恐形势很严峻,特别是美国“9.11事件”以后,国际恐怖势力更是有恃无恐,制造了多起骇人听闻的恐怖事件.基于此,各国都十分担心恐怖势力会对 本国社会造成的不稳

hdu 1210 求置换循环节

这个题的置换恰好是有规律的,所以也不用把置换给存下来,然后只要求出置换的循环节就可以了. 1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 using namespace std; 6 7 const int N = 200001; 8 bool visit[N]; 9 10 int gcd( int x, int y ) 11 {