您的位置:首页 > 其它

Back to Edit Distance(LCS + LIS)

2015-08-26 21:46 225 查看
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了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: