bnuoj_20924 Permutations
2016-07-07 18:16
274 查看
Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and
is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it
at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
input:
The first line contains a single integer n (1 ≤ n ≤ 2·105)
— the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will
appear in the permutation exactly once.
Next line describe the second permutation in the same format.
output:
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
sample input:
3
3 2 1
1 2 3
5
1 2 3 4 5
1 5 2 3 4
5
1 5 2 3 4
1 2 3 4 5
sample output:
2
1
3
//相当于寻找前面几个数有序的情况下后面的数插入
#include<stdio.h>
int a[200010],b[200010];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int k=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
if(b[i]==a[k]) k++;
}
printf("%d\n",n-k+1);
return 0;
}
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and
is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it
at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
input:
The first line contains a single integer n (1 ≤ n ≤ 2·105)
— the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will
appear in the permutation exactly once.
Next line describe the second permutation in the same format.
output:
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
sample input:
3
3 2 1
1 2 3
5
1 2 3 4 5
1 5 2 3 4
5
1 5 2 3 4
1 2 3 4 5
sample output:
2
1
3
//相当于寻找前面几个数有序的情况下后面的数插入
#include<stdio.h>
int a[200010],b[200010];
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int k=1;
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
if(b[i]==a[k]) k++;
}
printf("%d\n",n-k+1);
return 0;
}
相关文章推荐
- 相对路径的获取拼接
- OpenSSL - 利用OpenSSL自签证书和CA颁发证书
- 网络库crash以及boost asio strand dispath分析
- __stdcall,__cdecl,__fastcall的区别
- 在Xcode中使用Git进行源码版本控制
- Android 通过JNI实现守护进程,使Service服务不被杀死
- ACM中的博弈论入门(二) POJ 2960 SG 函数的应用
- xstream 别名的用法
- Length of Last Word
- _fastcall
- 【Android】SQLite 数据库基本操作
- 关于两个php.ini的说明
- Centos6.5 python升级成2.7版本出现的一些问题解决方法
- PNP型三极管____本人备注
- 声学
- CNN 卷积神经网络-- 残差计算
- 复习正则表达式时的一个小知识点
- Linux内核中的fastcall和asmlinkage宏
- Android实战技巧之十二:Android Studio导入第三方类库、jar包和so库
- [NOIP1999]拦截导弹