zoukankan      html  css  js  c++  java
  • Back to Edit Distance(LCS + LIS)

    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
    " , cas , (n-LIS ())*2) ;
            }
            return 0 ;
    }
    

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

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

  • 相关阅读:
    ArrayList实现原理及源码分析之JDK8
    红黑树原理和算法介绍
    TreeMap实现原理及源码分析之JDK8
    HashMap实现原理及源码分析之JDK8
    mysql索引的使用
    HashMap实现原理及源码分析之JDK7
    arthas Can not find tools.jar 使用报错
    idea maven 更新不到本地 手动添加的 jar
    Nodejs安装及环境配置
    安装独立版本的MAT
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4761763.html
Copyright © 2011-2022 走看看