hdu 5495 LCS (bestcoder #58 1002)
来源:互联网 发布:centos系统指令 编辑:程序博客网 时间:2024/05/17 07:58
LCS
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn} . Both sequences are permutation of {1,2,...,n} . You are going to find another permutation {p1,p2,...,pn} such that the length of LCS (longest common subsequence) of {ap1,ap2,...,apn} and {bp1,bp2,...,bpn} is maximum.
Input
There are multiple test cases. The first line of input contains an integer T , indicating the number of test cases. For each test case:
The first line contains an integern(1≤n≤105) - the length of the permutation. The second line contains n integers a1,a2,...,an . The third line contains n integers b1,b2,...,bn .
The sum ofn in the test cases will not exceed 2×106 .
The first line contains an integer
The sum of
Output
For each test case, output the maximum length of LCS.
Sample Input
231 2 33 2 161 5 3 2 6 43 6 2 4 5 1
Sample Output
24
出题人的解题思路:
Problem 1. LCS
题目中给出的是两个排列, 于是我们我们可以先把排列分成若干个环, 显然环与环之间是独立的. 事实上对于一个长度为l(l>1)的环, 我们总可以得到一个长度为l−1的LCS, 于是这个题的答案就很明显了, 就是n减去长度大于
1的环的数目.
其实就是把数组a到b看成一条边,由于都是1~n,所以判断有多少个独立环,长度为一的时候特判,大于一的时候为l-1;
ps: 输入用scanf,用cin会超时;
代码:
#include <iostream>#include <string.h>#include <algorithm>#include <stdio.h>using namespace std;struct node{ int a,b;}data[100005];int cmp(const node &s1,const node &s2){ return s1.a<s2.a;}int main(){ int vis[100005]; int t; cin>>t; while(t--) { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&data[i].a); for(int j=1;j<=n;j++) scanf("%d",&data[j].b); sort(data+1,data+n+1,cmp); memset(vis,0,sizeof(vis)); int ans=0; for(int i=1;i<=n;i++) { if(!vis[i]) { int p=i,l=1; while(data[p].b!=i) { l++; p=data[p].b; vis[p]=1; } vis[i]=1; if(l==1) ans+=l; else ans+=(l-1); } } cout<<ans<<endl; } return 0;}
0 0
- hdu 5495 LCS (bestcoder #58 1002)
- BestCoder Round #58 LCS
- HDU 5495 LCS(其实就是找环)——BestCoder Round #58(div.1 div.2)
- hdu 5495 BestCoder Round #58 (div.2) 1002
- HDU 5495 LCS
- hdu 5495 LCS
- HDU 5495 LCS
- hdu 5495 LCS dfs
- hdu 5495 LCS 置换
- HDU 5495 LCS
- hdu 5495 LCS(贪心)
- HDU 5495:LCS
- hdu 5495 LCS
- BestCoder Round #58 LCS 即hdu5495 (模拟)
- HDU 5495 LCS (置换)
- hdu 5495 LCS 置换群
- HDU 5495 LCS (并查集判环)
- hdu(5495)——LCS
- lambda表达式,结合map、reduce、filter函数
- iOS几种界面跳转方式简介
- Hadoop YARN架构设计要点
- c++原型模式
- Browser 相关内容
- hdu 5495 LCS (bestcoder #58 1002)
- Edison install ROS in two method
- 动态链接库与静态链接库的区别
- QT5 学习之路18---对话框数据传递
- iOS开发之导出ipa文件以及查看内容
- hdu 5496 Beauty of Sequence 枚举
- [about myself] 关于我自己
- 例题 7-7 宝箱 UVa 12325
- VS2010+PCL1.6.0+1.7.2(Win7 64位)安装教程