Square Root of Permutation - CF612E
Description
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, q = [4, 5, 1, 2, 3] is a permutation. For the permutation q the square of permutation is the permutation p that p[i] = q[q[i]] for each i = 1... n. For example, the square of q = [4, 5, 1, 2, 3] is p = q2 = [2, 3, 4, 5, 1].
This problem is about the inverse operation: given the permutation p you task is to find such permutation q that q2 = p. If there are several such q find any of them.
Input
The first line contains integer n (1 ≤ n ≤ 106) — the number of elements in permutation p.
The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the elements of permutation p.
Output
If there is no permutation q such that q2 = p print the number "-1".
If the answer exists print it. The only line should contain n different integers qi (1 ≤ qi ≤ n) — the elements of the permutation q. If there are several solutions print any of them.
Sample Input
4
2 1 4 3
3 4 2 1
4
2 1 3 4
-1
5
2 3 4 5 1
4 5 1 2 3
简单题意
给出一个大小为n的置换群p,求一个置换群q,使得q^2=p
胡说题解
首先我们观察q^2是怎么运算的,置换群可以看成一个一个的环,每个点i向a[i]连一条边就是那个图
q^2其实就是把i的边变成a[a[i]],也就是在环上走两步,然后q原本的环就变了
1.假设原来是奇数环,那么后来还是一个奇数环,只是顺序变了
2.假设原来是偶数环,那么就会拆成两个大小为一半的环
我们再看p
p上的奇数环可能是原来的奇数环,也有可能是偶数环拆开得来的
p上的偶数环只可能是原来的偶数环拆开得来
对于奇数环我们只要把这个环的后半部分与前半部分(先把这个环断开)交替插入就可以构造出原来的那个奇数环
对于偶数环我们就只能找一个相同大小的偶数环交替插入,即两个相同大小的偶数环合并,如果找不到相同大小的偶数环,那么我们就知道不存在这样的q使得q^2=p
#include<cstdio>
#include<algorithm>
using namespace std; const int maxn=; int n,tot,a[maxn],b[maxn],s[maxn],l[maxn],cir[maxn];
bool flag[maxn]; bool com(int a,int b){
return l[a]<l[b];
} int main(){
scanf("%d",&n);
int i,j,k;
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=n;i++)
if(!flag[i]){
cir[++tot]=i;
flag[i]=true;
++l[i];
j=a[i];
while(!flag[j]){
flag[j]=true;
++l[i];
j=a[j];
}
}
sort(cir+,cir++tot,com);
int x=;
bool f=true;
for(i=;i<=tot;i++)
if((l[cir[i]]&)== ){
if(x==)x=l[cir[i]];
else
if(x==l[cir[i]])x=;
else f=false;
}
if(x!=)f=false;
if(f==false)printf("-1");
else{
for(i=;i<=tot;i++){
if((l[cir[i]]&)==){
j=cir[i];
k=;
while(flag[j]){
s[k]=j;
flag[j]=false;
k=(k+)%l[cir[i]];
j=a[j];
}
for(j=;j<l[cir[i]]-;j++)b[s[j]]=s[j+];
b[s[l[cir[i]]-]]=s[];
}
else{
j=cir[i];
k=cir[i+];
while(flag[j]){
b[j]=k;
b[k]=a[j];
flag[j]=false;
flag[k]=false;
j=a[j];
k=a[k];
}
++i;
}
}
for(i=;i<=n;i++)printf("%d ",b[i]);
}
return ;
}
AC代码
Square Root of Permutation - CF612E的更多相关文章
- Codeforces 612E - Square Root of Permutation
E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...
- CF612E Square Root of Permutation
题目分析 我们首先模拟一下题意 假设有一个 \(q _1\) \(p\) \(a_1\) \(a_x\) \(a_{a_1}\) \(a_{a_x}\) \(q\) \(x\) \(a_1\) \(a ...
- codefroces 612E Square Root of Permutation
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...
- [CF 612E]Square Root of Permutation
A permutation of length n is an array containing each integer from 1 to n exactly once. For example, ...
- Codeforces.612E.Square Root of Permutation(构造)
题目链接 \(Description\) 给定一个\(n\)的排列\(p_i\),求一个排列\(q_i\),使得对于任意\(1\leq i\leq n\),\(q_{q_i}=p_i\).无解输出\( ...
- Codeforces 715A. Plus and Square Root[数学构造]
A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Project Euler 80:Square root digital expansion 平方根数字展开
Square root digital expansion It is well known that if the square root of a natural number is not an ...
- Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- (Problem 57)Square root convergents
It is possible to show that the square root of two can be expressed as an infinite continued fractio ...
随机推荐
- [数据结构]_[C/C++]_[链表的最佳创建方式]
场景 1.链表在C/C++里使用非常频繁, 因为它非常使用, 可作为天然的可变数组. push到末尾时对前面的链表项不影响. 反观C数组和std::vector, 一个是静态大小, 一个是增加多了会对 ...
- 【公司动态添加行】前台穿一个json的字符串到后台,并解析
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- 感觉总结了一切python常见知识点,可直接运行版本
#encoding=utf-8#http://python.jobbole.com/85231/#作用域a=1def A(a): a=2 print 'A:',a def B(): print 'B: ...
- Unity操作小技巧
1.操作类 1)F:选择物体后聚焦 2)V:选择物体的顶点,顶点吸附 3)Ctrl:摁住后拖动物体,可以按照系统设置的步长进行移动(Edit -> Snap setting) 4)Q W E R ...
- 了解Python控制流语句——while 语句
while 语句 Python 中 while 语句能够让你在条件为真的前提下重复执行某块语句. while 语句是 循环(Looping) 语句的一种.while 语句同样可以拥有 else 子句作 ...
- Leetcode-跳跃游戏
跳跃游戏 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] ...
- 机器学习之支持向量机(Support Vector Machine)
转载请注明出处:http://www.cnblogs.com/Peyton-Li/ 支持向量机 支持向量机(support vector machines,SVMs)是一种二类分类模型.它的基本模型是 ...
- 2.hbase原理(未完待续)
hbase简介相关概念hmsterhregionserver表regionhstorememstorestorefilehfileblockcacheWALminorcompactmajorcompa ...
- Python中的Numeric
整型Integer 在Python2.X中,Integer有两种类型,一种是32bit的普通类型,一种是精度无限制的long类型,在数字后面标识l或者L来标识long类型,并且,当32bit发生ove ...
- Lecture Sleep(尺取+前缀和)
Description 你的朋友Mishka和你参加一个微积分讲座.讲座持续n分钟.讲师在第i分钟讲述ai个定理. 米什卡真的对微积分很感兴趣,尽管在演讲的所有时间都很难保持清醒.给你一个米什卡行 ...