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 integer n(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 of n in the test cases will not exceed 2×106.

 
Output
For each test case, output the maximum length of LCS.
 
题意:给你两串数字然后让你求一个顺序使得这两个序列按这种顺序排列后LCS最大,并输出LCS的长度。
 
由于这题的数据大用LCS肯定不行,枚举方案肯定也不行。但是由于可以随意移动且数字是1~n中的所有数,所以可以将关联的数字求出来,
这些关联的数最大能组成len-2的LCS序列拿例题打个比方。
6
1 5 3 2 6 4
3 6 2 4 5 1
关联后可以得到
(1-3-2-4-1),(5-6-5)
1 3 2 4       5 6
3 2 4 1       6 5
所以组合后LCS就为(5-2)+(3-2)=4
还有如果只有两个如(5-5)这时候加1就行
 
 
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 1e5 + 10;
int a[M] , b[M];
int f[M] , va[M];
void init(int n) {
for(int i = 0 ; i <= n ; i++) {
f[i] = i;
va[i] = 1;
}
}
int find(int x) {
if(x != f[x])
f[x] = find(f[x]);
return f[x];
}
void Union(int x , int y) {
int xx = find(x);
int yy = find(y);
if(xx != yy) {
f[x] = yy;
va[yy] += va[xx];
}
}
int main()
{
int t;
scanf("%d" , &t);
while(t--) {
int n;
scanf("%d" , &n);
init(M);
for(int i = 0 ; i < n ; i++) {
scanf("%d" , &a[i]);
}
for(int i = 0 ; i < n ; i++) {
scanf("%d" , &b[i]);
Union(a[i] , b[i]);
}
int count = 0;
for(int i = 0 ; i < n ; i++) {
if(f[a[i]] == a[i]) {
if(va[a[i]] == 1) {
count++;
}
else {
count += (va[a[i]] - 1);
}
}
}
printf("%d\n" , count);
}
return 0;
}

最新文章

  1. Maven-setting.xml详解
  2. 自定义cell的步骤(每个cell的高度不一样,每个cell里面显示的内容也不一样)
  3. [BZOJ2724][Violet 6]蒲公英
  4. Workflow_如何处理标准异常和自定义异常(案例)
  5. Redis多机功能总结
  6. R语言处理大规模数据集的编程要点
  7. oc-10-函数与方法的区别
  8. Django 数据库查询优化
  9. maven第四章背景案例
  10. Linux 远程查看tomcat控制台
  11. socketlog的安装和使用
  12. linux同步与通信
  13. System Error. Code:1722. RPC服务器不可用解决办法
  14. MyBatis学习日记(二): MyBatis Say Hello
  15. .Net Core AES加密解密
  16. python数据图形化—— matplotlib 基础应用
  17. 简析Window、Activity、DecorView以及ViewRoot之间的错综关系
  18. MPD大会北京上海两站圆满落幕
  19. 在Fragment里面调用getActivity()报null
  20. 学习技巧-如何在IBM官网寻找学习资料

热门文章

  1. 工业物联网网关在线探测之TraceRoute
  2. .net core开发从未如此简单,比abp更接地气
  3. GStreamer流媒体知识介绍
  4. React之动画实现
  5. 【Java笔记】【Java核心技术卷1】chapter3 D2注释
  6. javascript基础案例解析
  7. SonarQube系列一、Linux安装与部署
  8. Vsftp服务器配置文件详解
  9. WIN10家庭版桌面右键单击显示设置出现ms-settings:display或ms-settings:personalization-background解决办法[原创]
  10. vue-cli+v-charts实现移动端可视化图表