CF612E Square Root of Permutation
题目分析
我们首先模拟一下题意
假设有一个 \(q _1\)
\(p\) | \(a_1\) | \(a_x\) | \(a_{a_1}\) | \(a_{a_x}\) |
---|---|---|---|---|
\(q\) | \(x\) | \(a_1\) | \(a_x\) | \(a_{a_1}\) |
\(pos\) | 1 | \(x\) | \(a_1\) | \(a_x\) |
对这个表格分析,发现,当前节点的后继为前驱的对应
如果我们对 \(i \to p_i\) 建边,模拟一下,发现
偶环是会有两个环组成,而奇环会形成一个类似于五角星的图形
于是,我们可以找出所有环,对于奇环模拟即可,而偶环,拆分后再访问
#include<bits/stdc++.h>
using namespace std;
int n;
int a[1000005];
int vis[1000005];
struct node{
vector<int>vs;
}scc[1000005];
int cnt_block;
void dfs(int x,int key)
{
if(vis[x])
{
return;
}
vis[x]=1;
scc[key].vs.push_back(x);
dfs(a[x],key);
}
bool cmp(node x,node y)
{
return x.vs.size()<y.vs.size();
}
vector<int>temp;
int ans[1000005];
int pcd[1000005];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
++cnt_block;
dfs(i,cnt_block);
}
}
//
sort(scc+1,scc+1+cnt_block,cmp);
for(int i=1;i<=cnt_block;i++)
{
// printf("%d\n",scc[i].vs.size());
temp.clear();
if(scc[i].vs.size()&1)
{
int mid = (scc[i].vs.size() + 1)/2;
for (int j =1;j <= mid; j++)
{
pcd[(j*2)-1] = scc[i].vs[j-1];
}
for (int j = mid + 1;j <= scc[i].vs.size(); j++)
{
pcd[(j - mid)*2] = scc[i].vs[j-1];
}
for(int j=1;j<=scc[i].vs.size();j++)
{
ans[pcd[j]]=pcd[j+1];
}
ans[pcd[scc[i].vs.size()]]=pcd[1];
}
else
{
if(i==cnt_block)
{
printf("-1");
return 0;
}
if(scc[i].vs.size()^scc[i+1].vs.size())
{
printf("-1");
return 0;
}
for(int j=0;j<scc[i].vs.size();j++)
{
temp.push_back(scc[i].vs[j]);
temp.push_back(scc[i+1].vs[j]);
}
for(int j=0;j<temp.size()-1;j++)
{
ans[temp[j]]=temp[j+1];
}
ans[temp[temp.size()-1]]=temp[0];
i++;
}
}
for(int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
}
CF612E 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 ...
- Square Root of Permutation - CF612E
Description A permutation of length n is an array containing each integer from 1 to n exactly once. ...
- 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 ...
随机推荐
- Oracle bulk collect into 的几种用法
bulk collect 和 forall 联合应用写起来显得有些啰嗦,不过为了速度,多写两句又何妨 建立两个临时表 create table T_TEST ( TESTID NUMBER(19) n ...
- Function overloading and return type
In C++ and Java, functions can not be overloaded if they differ only in the return type. For example ...
- Spring(4):Mybatis和Spring整合
第一步:创建数据库 MySQL代码 1 CREATE DATABASE `mybatis` ; 2 3 USE `mybatis`; 4 5 CREATE TABLE `user` ( 6 `id` ...
- 【Java多线程】Java 原子操作类API(以AtomicInteger为例)
1.java.util.concurrent.atomic 的包里有AtomicBoolean, AtomicInteger,AtomicLong,AtomicLongArray, AtomicRef ...
- 利用代码覆盖率提高嵌入式软件的可靠性 - VectorCAST
简介 代码覆盖率是衡量软件测试完成情况的指标,通常基于测试过程中已检查的程序源代码比例 计算得出.代码覆盖率可以有效避免包含未测试代码的程序被发布. 代码覆盖率能不能提高软件的可靠性?答案是肯定的,代 ...
- Python enumerate():使用计数器简化循环
摘要:当您需要计数和迭代中的值时,Pythonenumerate()允许您编写 Pythonicfor循环.最大的优点enumerate()是它返回一个带有计数器和值的元组,因此您不必自己增加计数器. ...
- HashMap的putAll方法介绍说明
jdk1.8 使用putAll时,新map中的值仅为旧map值所对应对象的引用,并不会产生新对象. 如下,使用for循环赋值! public void putAll(Map<? extends ...
- Docker从入门到精通(三)——概念与执行流程
前面我们大概介绍了docker是什么以及如何安装docker,但是对里面出现的一些名词,可能大家还不熟悉,这篇文章就来为大家解惑. 1.容器化平台 Docker 是提供应用打包,部署与运行应用的容器化 ...
- 【死磕Java并发】—–深入分析volatile的实现原理
通过前面一章我们了解了synchronized是一个重量级的锁,虽然JVM对它做了很多优化,而下面介绍的volatile则是轻量级的synchronized.如果一个变量使用volatile,则它比使 ...
- selenium 控制窗口无限向下滚动
使用脚本 window.scrollBy(0, 1000),放入 while True 循环,示例代码: while True: js = 'window.scrollBy(0, 1000)' dri ...