hdu-5495 LCS(置换)
题目链接:
LCS
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 818 Accepted Submission(s): 453
The first line contains an integer n(1≤n≤105) - 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 2×106.
3
1 2 3
3 2 1
6
1 5 3 2 6 4
3 6 2 4 5 1
4
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,a[maxn],b[maxn],pos[maxn],vis[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),pos[a[i]]=i,vis[i]=0;
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
int ans=0;
for(int i=1;i<=n;i++)
{
if(!vis[b[i]])
{
vis[b[i]]=1;
int len=0,fa=b[i],p;
while(1)
{
p=pos[fa];
fa=b[p];
len++;
if(vis[fa])break;
vis[fa]=1;
}
if(len==1)ans++;
else ans+=len-1;
}
}
printf("%d\n",ans);
}
return 0;
}
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(并查集)
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 ...
随机推荐
- [转载]hashmap hashtable 的区别
Hashtable 和 HashMap 做为 Map 的基本特性 两者都实现了Map接口,基本特性相同 - 对同一个Key,只会有一个对应的value值存在 - 如 ...
- angularjs post
/** * POST 1 * $http.post('http://localhost:8001/quickstart/task/create', { newTask: newTask }) */ / ...
- springmvc+mybatis+spring 整合
获取[下载地址] [免费支持更新]三大数据库 mysql oracle sqlsever 更专业.更强悍.适合不同用户群体[新录针对本系统的视频教程,手把手教开发一个模块,快速掌握本系统] ...
- [ASP.NET MVC] Model Binding With NameValueCollectionValueProvider
[ASP.NET MVC] Model Binding With NameValueCollectionValueProvider 范例下载 范例程序代码:点此下载 问题情景 一般Web网站,都是以H ...
- IFeatureCursor.Flush
IFeatureCursor.Flush Method Flush any outstanding buffered writes to the database. [Visual Basic .NE ...
- 由于无法在数据库 'TestNonContainedDB' 上放置锁 ALTER DATABASE 失败
Error: 消息5601,级别16,状态1,第1行,由于无法在数据库 'TestNonContainedDB' 上放置锁,ALTER DATABASE 失败.请稍后再试.消息5069,级别16,状态 ...
- Android 开发前的基本的配置及第一个Android 程序
一.JDK 1. 网上下载JDK 2. 配置环境变量(网上百度相关资料) JAVA_HOME=D:\Java\jdk1.8.0_91 CLASSPATH=.;%JAVA_ ...
- Android Small插件化框架解读——Activity注册和生命周期
通过对嵌入式企鹅圈原创团队成员degao之前发表的<Android Small插件化框架源码分析>的学习,对Android使用的插件化技术有了初步的了解,但还是有很多需要认真学习的地方,特 ...
- Charles使用详情
Charles各版本下载: Charles for Windows 32 bit Charles for Windows 64 bit Charles for linux Charles for Ma ...
- win7 解决git clone 连接被拒绝—hosts文件过期
我出现问题的原因是自己修改过 C:\Windows\System32\drivers\etc\HOSTS, 把同事的这个文件拷贝过来后,一切恢复了正常 错误在eclipse中表现为: ...