hdu 5495 LCS
You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are permutation of {,,...,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.
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 integer n(≤n≤) - the length of the permutation. The second line contains n integers a1,a2,...,an. The third line contains nintegers b1,b2,...,bn. The sum of n in the test cases will not exceed ×.
For each test case, output the maximum length of LCS.
题意:
给出1~n的两个排序a[n],b[n],求用某个排序p[n],使得a[p[0]],a[p[1]],a[p[2]]······与b[p[0]],b[p[1]],b[p[2]]······的最长公共子序列取得最大值,求最终取得的LCS多大。
思路:
LCS一定会tle的,关键就是这是1~n的序列,所以必然a[],b[]存在相同的数字,也就是一定会由一些循环节组成,而每个循环节经过排列首尾相接一定会有只相错一位的情况,最终的LCS也就是总长减去循环节的个数,特判自环不减就行了。直接深搜写的,跑得比较慢。
来自官方题解:
题目中给出的是两个排列, 于是我们我们可以先把排列分成若干个环, 显然环与环之间是独立的. 事实上对于一个长度为l(l>1)的环, 我们总可以得到一个长度为l−1的LCS, 于是这个题的答案就很明显了, 就是n减去长度大于1的环的数目.
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 100006
#define inf 1e12
int n;
int a[N];
int b[N];
int vis[N];
bool dfs(int x){
int tmp=b[x];
if(!vis[tmp]){
vis[tmp]=;
dfs(a[tmp]);
return true;
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
int x;
for(int i=;i<=n;i++){
scanf("%d",&x);
b[x]=i;
}
int ans=; for(int i=;i<=n;i++){
if(!vis[i]){
vis[i]=;
if(!dfs(a[i])){
ans--;
}
ans++;
}
}
printf("%d\n",n-ans);
}
return ;
}
hdu 5495 LCS的更多相关文章
- hdu 5495 LCS 水题
LCS Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5495 Descr ...
- hdu 5495 LCS(并查集)
Problem Description You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are ...
- hdu 5495 LCS (置换群)
Sample Input231 2 33 2 161 5 3 2 6 43 6 2 4 5 1 Sample Output24 C/C++: #include <map> #includ ...
- HDU 5495:LCS
LCS Accepts: 127 Submissions: 397 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/655 ...
- hdu 1423(LCS+LIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 好坑啊..还有公共串为0时的特殊判断,还有格式错误..看Discuss看知道除了最后一组测试数据 ...
- hdu 1503 LCS输出路径【dp】
hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左 ...
- hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏
a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...
- Advanced Fruits(HDU 1503 LCS变形)
Advanced Fruits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 1080(LCS变形)
Human Gene Functions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
随机推荐
- Java迭代器深入理解及使用
Iterator(迭代器) 作为一种设计模式,迭代器可以用于遍历一个对象,对于这个对象的底层结构开发人员不必去了解. java中的Iterator一般称为“轻量级”对象,创建它的代价是比较小的.这里笔 ...
- linux指定动态运行库的位置
动态运行库在windows.linux下均广泛使用.windows下通常为dll文件,linux下为so文件.不过,对于部署程序,这两个系统查找依赖的运行库文件时却不一样.对于windows而言,优先 ...
- [转]Laravel 4之Eloquent ORM
Laravel 4之Eloquent ORM http://dingjiannan.com/2013/laravel-eloquent/ 定义Eloquent模型 模型通常放在app/models目录 ...
- 几个Linux常见命令
ls 作用:查看目录下的文件 格式:直接ls查看当前所在目录,或者 ls 参数 /目录名 参数: -l 查看详细信息 -a 显示隐藏文件 . 表示当前目录 .. 上级目录 -G 用不同颜色标记 ...
- Https协议简析及中间人攻击原理
1.基础知识 1.1 对称加密算法 对称加密算法的特点是加密密钥和解密密钥是同一把密钥K,且加解密速度快,典型的对称加密算法有DES.AES等 ...
- html进阶css(5)
css定位机制 css有三种基本的定位机制:普通流,浮动和绝对定位. 除非专门指定,否则所有框都在普通流中定位.也就是说,普通流中的元素的位置由元素在html中的位置决定. 块级框从上到下一个接一个的 ...
- 基础命名空间:序列化 System.Runtime.Serialization
对象通常都有状态(state),从一个对象中抽取这种状态,不论是将它存储于某地,还是通过网络传送,这种抽取动作称为“将一个对象序列化”,而反向处理过程,从一个被序列化的状态重建一个对象即为反序列化. ...
- nginx搭建flv、mp4流媒体服务
1.安装pcre-8.33.tar.bz2 #tar -xvf 1.pcre-8.33.tar.bz2 #cd pcre-8.33/ #./configure #make && mak ...
- Spark配置&启动脚本分析
本文档基于Spark2.0,对spark启动脚本进行分析. date:2016/8/3 author:wangxl Spark配置&启动脚本分析 我们主要关注3类文件,配置文件,启动脚本文件以 ...
- (转)JAVA路径问题及命令行编译运行基础(linux下)
原地址: http://blog.csdn.net/biaobiaoqi/article/details/6846274 java的运行机制的基本概念: 源文件 也就是我们熟知的.java文件. 类文 ...