HDU 5495 LCS (置换)
来源:互联网 发布:四海金戈 全武将数据 编辑:程序博客网 时间:2024/05/16 13:02
题意:
求一个置换,使得置换后的两个序列的LCS最大,求这个最大LCS
分析:
我们手算可以发现第一序列对于第二个序列的置换,可以把序列分成若干个循环节环,显然环与环之间是独立的.
事实上对于一个长度为l(l>1)的环,我们总可以得到一个长度为l−1的LCS
那么答案就是ans=∑max{1,l−1}
记得先排序−−求循环节是从1−n
代码:
//// Created by TaoSama on 2015-10-03// Copyright (c) 2015 TaoSama. All rights reserved.////#pragma comment(linker, "/STACK:1024000000,1024000000")#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;#define pr(x) cout << #x << " = " << x << " "#define prln(x) cout << #x << " = " << x << endlconst int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;int n, a[N], b[N];bool vis[N];int main() {#ifdef LOCAL freopen("in.txt", "r", stdin);// freopen("out.txt","w",stdout);#endif ios_base::sync_with_stdio(0); int t; scanf("%d", &t); while(t--) { scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d", a + i); for(int i = 1; i <= n; ++i) { int x; scanf("%d", &x); b[a[i]] = x; } memset(vis, false, sizeof vis); int ans = 0; for(int i = 1; i <= n; ++i) { if(!vis[i]) { int j = i, cnt = 0; while(!vis[j]) { ++cnt; vis[j] = true; j = b[j]; } ans += max(1, cnt - 1); } } printf("%d\n", ans); } return 0;}
0 0
- HDU 5495 LCS (置换)
- hdu 5495 LCS 置换
- HDU 5495 LCS (置换群)
- HDU 5495 LCS(置换群)
- hdu 5495 LCS 置换群
- HDU 5495 置换群
- HDU 5495(置换群)
- hdu 5495 置换
- HDU5495--LCS(置换群)
- 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
- hdoj 5495 LCS 【统计元素个数大于1的置换群数目】【LCS变形】
- OpenGL 缺省视景体是中心在原点,边长为2的立方体
- torcs源码分析--(1)工程分析
- UVA208在一个图中,给定终点和起点,搜索路线,巧妙剪枝
- 航电 acm 2013
- torcs源码分析--(2)关键文件及结构
- HDU 5495 LCS (置换)
- torcs源码分析--(3)总览
- 基于Android的图案解锁实现
- php 基于PDO下的exec()函数查询
- lua脚本的基本语法(二)
- Mount VDI as logical drive on host
- java.lang.NoSuchFieldError: count at org.eclipse.jface.viewers.ColumnViewer$1.mouseDown
- bzoj3714【PA2014】Kuglarz
- 例题5.13 战争和物流 LA4080