码迷,mamicode.com
首页 > 其他好文 > 详细

Back to Edit Distance(LCS + LIS)

时间:2015-08-26 23:50:20      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:

Given 2 permutations of integers from 1 to N, you need to find the minimum number of operations necessary to change both or any one of them in such a way that they become exactly same. Here only two operations are allowed: either you can delete an integer from any position or you can insert an integer into any position, but replacing one integer by another one is not allowed. Say, N = 5 and the permutations are {1, 3, 5, 4, 2} and {1, 5, 4, 3, 2}. Then we need just 2 operations: we need to delete 3 from the 2nd position and insert it in the 4th position of the first permutation, or we can delete 3 from both the permutations, which also needs two operations.

Input

First line of the input contains a positive integer T (T ≤ 40). Each of the following T cases contains 3 lines for each case: the 1st line contains a single integer N (1 ≤ N ≤ 200, 000) and the next two lines contain the two permutations of the integers.

Output

For each case, print a line of the form ‘Case < x >: < y >’, where x is the case number and y is the number of operations necessary to covert the 1st permutation to the 2nd permutation.

Sample Input

2 5 1 3 5

4 2 1 5 4

3 2 4 1 2

4 3 3 4 2 1

Sample Output

Case 1: 2

Case 2: 6

 

#include<bits/stdc++.h>
using namespace std;
const int M = 2e5 + 10 , inf = 0x3f3f3f3f;
int n ;
int orm[M] ;
int a[M] ;
int Top[M] ;
int judge (int x) {
        int l = 0 , r = n ; 
        int ret = l ;
        while (l <= r) {
                int mid = l+r >> 1 ;
                if (x > Top[mid]) {
                        ret = mid ;
                        l = mid+1 ;
                }
                else r = mid-1 ;
        }
        Top[ret+1] = min (Top[ret+1] , x) ;
        return ret+1 ;
}

int LIS () {
        int ans = 0 ;
        for (int i = 1 ; i <= n ; i ++) {
                ans = max (ans , judge (a[i])) ;
        }
        return ans ;
}

int main () {
        int T ;
        scanf ("%d" , &T ) ;
        for (int cas = 1 ; cas <= T ; cas ++) {
                scanf ("%d" , &n) ;
                for (int i = 1 ; i <= n ; i ++) {
                        int x ;
                        scanf ("%d" , &x) ;
                        orm[x] = i ;
                        Top[i] = inf ;
                }
                for (int j = 1 ; j <= n ; j ++) {
                        int x ;
                        scanf ("%d" , &x) ;
                        a[j] = orm[x] ;
                }
                printf ("Case %d: %d\n" , cas , (n-LIS ())*2) ;
        }
        return 0 ;
}

  要灵活运用他是一个1~n的排列。

然后你就能把lcs变成lis了。

Back to Edit Distance(LCS + LIS)

标签:

原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4761763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!