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 ...
随机推荐
- nginx配合modsecurity实现WAF功能
一.准备工作 系统:centos 7.2 64位.nginx1.10.2, modsecurity2.9.1 owasp3.0 1.nginx:http://nginx.org/download/ng ...
- python3学习笔记目录
目录: Python基础(一),Day1 python基础(二),Day2 python函数和常用模块(一),Day3 python函数和常用模块(二),Day4 python函数和常用模块(三),D ...
- 获取用户的真实ip
常见的坑有两个: 一.获取的是内网的ip地址.在nginx作为反向代理层的架构中,转发请求到php,java等应用容器上.结果php获取的是nginx代理服务器的ip,表现为一个内网的地址.php获取 ...
- css设置height 100%
需要显式设置html,body为100%,body是相对于html,wrapper是相对于body html,body{ height: 100%; } .wrapper{ height: 100; ...
- Android破解之Lic文件加密程序(首例)
我不会写Android,这是我第一个破解Android的例子,耗时接近一天,希望大神不要见笑! 本程序为商业软件,不便发布APK程序. 不要给我发消息,我不得回,有问题,直接回帖就可以了. 准备工作 ...
- Snort - manual 笔记(三)
1.6 Reading pcap files Snort 不仅可以监听interface, 还可以读取和分析已经捕获的数据包. 1.6.1 Command line arguments 下面的命令都可 ...
- Oracle递归查询
一.创建数据 1.1.建立表与插入数据 CREATE TABLE DISTRICT ( ID ) NOT NULL, PARENT_ID ), NAME BYTE) NOT NULL ); ALTER ...
- .NET下单文件的上传处理
ASP.NET的单文件上传使用控件 <asp:FileUpload ID="upmess" runat="server" Width="248p ...
- 使用docker搭建lnmp环境
Docker容器LNMP环境搭建 安装 制作镜像 启动并关联实例 安装 系统环境 硬件型号: ThinkPad T520 系统版本: ubuntu 14.04 CPU: i7 RAM: 8G 添加软件 ...
- windows 注册表编程
例子:将本地计算机的Monitor ID写入到注册表中 (1)获取MonitorID BOOLEAN DeviceMonitorService::EnumClassDevice(const GUID ...