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)的更多相关文章

  1. CJOJ 1070 【Uva】嵌套矩形(动态规划 图论)

    CJOJ 1070 [Uva]嵌套矩形(动态规划 图论) Description 有 n 个矩形,每个矩形可以用两个整数 a, b 描述,表示它的长和宽.矩形 X(a, b) 可以嵌套在矩形 Y(c, ...

  2. [LeetCode] 72. Edit Distance(最短编辑距离)

    传送门 Description Given two words word1 and word2, find the minimum number of steps required to conver ...

  3. Minimum edit distance(levenshtein distance)(最小编辑距离)初探

    最小编辑距离的定义:编辑距离(Edit Distance),又称Levenshtein距离.是指两个字串之间,由一个转成还有一个所需的最少编辑操作次数.许可的编辑操作包含将一个字符替换成还有一个字符. ...

  4. [LeetCode] Edit Distance(很好的DP)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  5. [leetcode72]Edit Distance(dp)

    题目链接:https://leetcode.com/problems/edit-distance/ 题意:求字符串的最短编辑距离,就是有三个操作,插入一个字符.删除一个字符.修改一个字符,最终让两个字 ...

  6. [HAOI2010]最长公共子序列(LCS+dp计数)

    字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk-1”是X ...

  7. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  8. 71.Edit Distance(编辑距离)

    Level:   Hard 题目描述: Given two words word1 and word2, find the minimum number of operations required ...

  9. HDU5812 Distance(枚举 + 分解因子)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5812 Description In number theory, a prime is a ...

随机推荐

  1. Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found

    今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...

  2. CF 701B Cells Not Under Attack(想法题)

    题目链接: 传送门 Cells Not Under Attack time limit per test:2 second     memory limit per test:256 megabyte ...

  3. 【Beta】第一次任务发布

    后端 整理以下5个环节的API文档:登录注册.收藏以及获取收藏.个人信息的获取以及更新.发送实验数据以获取PDF报告 验收条件:每个接口说明请求格式(请求方式,请求内容)和返回格式 例: 请求 方式: ...

  4. python03 面向对象的编程01

    话不多说,直接进入正文: 编程思想: 我个人所了解的有2种,还有个函数式编程的概念模模糊糊(大概是把常用的代码封装成一个函数,重复调用即可,先认为就是这样吧) 1 面向对象:面向对象是把所有的事务物当 ...

  5. BZOJ2827: 千山鸟飞绝

    离散化坐标,每个坐标开一棵以鸟的编号为关键字的平衡树.每次插入时打2个标记,同时更新自身.这个方法比较显然,而且好写.正解好像用很迷的方法乱搞了一波,然后用线段树不打标记就做出来了,并不会. trea ...

  6. orancle的安装和配置

    1.安装 Oracle 版本:Oracle Database 10g Release 2 (10.2.0.1) 下载地址: http://www.oracle.com/technology/softw ...

  7. JAVA第三周课后作业

    JAVA课后作业 一.枚举类型 代码: enum Size{SMALL,MEDIUM,LARGE}; public cl ass EnumTest { public static void main( ...

  8. C#--使用存储过程

    过程: 1.连接数据库 2.设置执行类型为存储过程,区别于SQL语句 3.得到执行结果,返回值.输出参数等 //连接数据库 string connstr = "Data Source=ZGC ...

  9. LyX-220-Installer-3

    所见即所得 单独安装这个写作业可以了,要发论文用CTeX Ctrl + M 打开数学输入,里面可以输入 TeX 代码

  10. ios 消息转发初探

    有时候服务器的接口文档上一个数据写的是string类型,这时候你就会直接把它赋值给一个label. 问题来了,有时候这个string的确是string,没有问题,有时候又是NSNumber,当然不管三 ...