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 ...
随机推荐
- spring-boot、spring-data-jpa、hibernate整合
一.Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置. ...
- HashMap在并发场景下踩过的坑
本文来自网易云社区 作者:张伟 关于HashMap在并发场景下的问题有很多人,很多公司遇到过!也很多人总结过,我们很多时候都认为这样都坑距离自己很远,自己一定不会掉入这样都坑.可是我们随时都有就遇到了 ...
- webpack中Development和Production模式的区分打包
当我们在开发一个项目的时候,我们一般用development这个环境进行项目的开发,在这个环境下,webpack使用dev-server,帮我们启用一个服务器,然后这个服务器里面还集成了一些,比如hm ...
- PyCharm添加Selenium与Appium类库
PyCharm添加Selenium与Appium依赖, 不需要用pip去安装!
- fiddler不经意的功能
捕获指定客户端的请求,直接食用 窗口分离,直接食用 Hide this column 隐藏此列Ensure all columns are visible 显示默认所有列Customize co ...
- Appium1.8及以上命令行启动
安装命令行启动版本的Appium,appium-doctor需要独立下载了,用 npm的话需要FQ才好使,所有安装了cnpm代替npm, cnpm是从淘宝的国内镜像下载 npm config rm p ...
- 【转】网游服务器中的GUID(唯一标识码)实现-基于snowflake算法
本文中的算法采用twitter的snowflake算法,具体请搜索介绍,原来是用Scala写的,因我项目需要,改写成C++语言,主要用于高效的生成唯一的ID, 核心算法就是毫秒级时间(41位)+机器I ...
- ionic 提示框
html文件 <ion-header> <ion-navbar> <ion-title>Toast</ion-title> </ion-navba ...
- 初涉 JavaScript
网页是什么 网页 = Html+CSS+JavaScriptHtml:网页元素内容CSS:控制网页样式JavaScript:操作网页内容,实现功能或者效果 JavaScirpt 发展历史 参考 使用 ...
- 浪在ACM新春大作战
题目链接: # Name 补题状态 A Memory and Crow 已补 B Memory and Trident 已补 C Memory and De-Evolution 已补 D Memory ...