Codeforces.612E.Square Root of Permutation(构造)
\(Description\)
给定一个\(n\)的排列\(p_i\),求一个排列\(q_i\),使得对于任意\(1\leq i\leq n\),\(q_{q_i}=p_i\)。无解输出\(-1\)。
\(1\leq n\leq10^6\)。
\(Solution\)
对排列\(q_i\)我们建一张图,边为\(i\to q_i\)。显然这张图是由几个环构成。
发现对于\(q_{q_i}\)的图,原来\(q_i\)中的奇环它们还是类似的一个奇环,原来的偶环会分裂成两个大小相等的偶环。
所以对\(p_i\)建图,找出里面的环,是奇环就把相邻点间隔为\(2\)地插入到环里,是偶环就找到和它一样大的一个合并,找不到就无解。这样就可以得到\(q_i\)的图了。(每个偶环只能合并一次→_→)
复杂度\(O(n)\)。
//249ms 21300KB
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 1000000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
typedef long long LL;
const int N=1e6+5;
int nxt[N],q[N],tmp[N],tmp2[N],id[N];
bool vis[N];
char IN[MAXIN],*SS=IN,*TT=IN;
inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-48,c=gc());
return now;
}
int main()
{
const int n=read();
for(int i=1; i<=n; ++i) nxt[i]=read();
for(int s=1; s<=n; ++s)
if(!vis[s])
{
int cnt=0,x=s;
do
{
tmp[++cnt]=x, vis[x]=1, x=nxt[x];
}while(x!=s);
if(cnt&1)
{
for(int i=1,now=0; i<=cnt; ++i,(now+=2)>=cnt&&(now-=cnt)) tmp2[now]=tmp[i];
for(int i=0; i<cnt; ++i) q[tmp2[i]]=tmp2[i+1];
q[tmp2[cnt-1]]=tmp2[0];
}
else
{
if(!id[cnt]) id[cnt]=s;
else
{
int y=id[cnt],x=y,t=0;
do
{
tmp2[++t]=x, x=nxt[x];
}while(x!=y);
for(int t1=1,t2=1,las=tmp[1],i=t<<1; i; --i)
las=q[las]=i&1?tmp[++t1]:tmp2[t2++];
q[tmp2[t]]=tmp[1], id[t]=0;
}
}
}
for(int i=2; i<=n; i+=2) if(id[i]) return puts("-1"),0;
for(int i=1; i<=n; ++i) printf("%d ",q[i]);
return 0;
}
Codeforces.612E.Square Root of Permutation(构造)的更多相关文章
- 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 ...
- [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, ...
- 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, ...
- Square Root of Permutation - CF612E
Description A permutation of length n is an array containing each integer from 1 to n exactly once. ...
- CF612E Square Root of Permutation
题目分析 我们首先模拟一下题意 假设有一个 \(q _1\) \(p\) \(a_1\) \(a_x\) \(a_{a_1}\) \(a_{a_x}\) \(q\) \(x\) \(a_1\) \(a ...
- 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 ...
- 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 ...
- Codeforces Round #372 (Div. 1) A. Plus and Square Root 数学题
A. Plus and Square Root 题目连接: http://codeforces.com/contest/715/problem/A Description ZS the Coder i ...
- Codeforces 417E Square Table(随机算法)
题目链接:Codeforces 417E Square Table 题目大意:给出n和m.要求给出一个矩阵,要求每一列每一行的元素的平方总和是一个平方数. 解题思路:构造.依照 a a a b a a ...
随机推荐
- Django标签&迭代&循环&过滤
1.{% for Person in persons %}模板标签的替换,就是利用了基础模板的底层设计,嵌套了其他显示的内容.常见的内容替换标签{% block content %}{%endbloc ...
- Linux----centos安装mysql
第一步wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 第二步rpm -ivh mysql-community-r ...
- numpy 与 pandas
numpy: import numpy as np np.array([1,2,3]) 创建数组 np.arange(10).reshape(2,5) 类似于range(起始,终止,步长),可以加re ...
- Python中的xxx == xx是否等价于xxx is xxx
先上代码: >>> a = 1 >>> b = 1 >>> a is b True >>> a == b True what? ...
- 使用sysbench 0.5 对mysql 进行性能、压力测试
sysbench是一个模块化的.跨平台.多线程基准测试工具,主要用于评估测试各种不同系统参数下的数据库负载情况.目前sysbench代码托管在launchpad上,项目地址:https://launc ...
- python字符串之join
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join():连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连 ...
- Nancy 寄宿IIS
一:简介 Nancy是一个轻量级的独立的框架,下面是官网的一些介绍: Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台,框架的目标是保持尽可能多的方 ...
- Lazy<T> 延迟加载
namespace ConsoleAppTest { class Program { static void Main(string[] args) { Lazy<Student> stu ...
- CallContext
1.线程本地存储区的专用集合对象,并提供对每个逻辑执行线程都唯一的数据槽.2.数据槽不在其他逻辑线程上的调用上下文之间共享. class Program { static Jason_TestEnti ...
- [转] 梦里Babel知多少(一)
平时开发中,经常需要用到ES6/ES7的语法.那么就需要用到Babel来对代码进行转码处理. 之前用Vue比较多,所以以Vue-cli作为参考来分析. 第一张图是几个月前的Vue-cli生成的 第二 ...