【构造】Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders
根据那两个式子
g(h(x))=x
h(g(x))=f(x)
可以推出来两个新的式子
g(f(x))=g(x)
h(x)=f(h(x))
于是,我们先找到f(x)的所有不动点,有几个不动点,m就是多少,将它们依次赋值给h(1),h(2),...,h(m),对应的g(h(i))赋值成1,2,..,m
然后如果对于某个i,g(f(i))不存在的话,则无解,否则有解,将g和h依次输出即可。
#include<cstdio>
using namespace std;
int n,f[100010],g[100010],h[1000010],m;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&f[i]);
for(int i=1;i<=n;++i)
if(f[i]==i)
{
h[++m]=i;
g[i]=m;
}
if(!m)
{
puts("-1");
return 0;
}
for(int i=1;i<=n;++i)
if(!g[f[i]])
{
puts("-1");
return 0;
}
printf("%d\n",m);
for(int i=1;i<n;++i)
printf("%d ",g[f[i]]);
printf("%d\n",g[f[n]]);
for(int i=1;i<m;++i)
printf("%d ",h[i]);
printf("%d\n",h[m]);
return 0;
}
【构造】Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders的更多相关文章
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造
D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造
A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders
地址:http://codeforces.com/contest/765/problem/D 题目: D. Artsem and Saunders time limit per test 2 seco ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set
F. Souvenirs 题目连接: http://codeforces.com/contest/765/problem/F Description Artsem is on vacation and ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C. Table Tennis Game 2 水题
C. Table Tennis Game 2 题目连接: http://codeforces.com/contest/765/problem/C Description Misha and Vanya ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B. Code obfuscation 水题
B. Code obfuscation 题目连接: http://codeforces.com/contest/765/problem/B Description Kostya likes Codef ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A. Neverending competitions 水题
A. Neverending competitions 题目连接: http://codeforces.com/contest/765/problem/A Description There are ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding
地址:http://codeforces.com/contest/765/problem/E 题目: E. Tree Folding time limit per test 2 seconds mem ...
随机推荐
- JQuery中的each()的使用
each()函数是基本上所有的框架都提供了的一个工具类函数,通过它,你可以遍历对象.数组的属性值并进行处理. jQuery和jQuery对象都实现了该方法,对于jQuery对象,只是把each方法简单 ...
- [hdu 3949]线性基+高斯消元
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 一开始给做出来的线性基wa了很久,最后加了一步高斯消元就过了. 之所以可以这样做,证明如下. 首 ...
- POJ1637:Sightseeing tour(混合图的欧拉回路)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10581 Accepted: 4466 ...
- System l类arraycopy的用法
package org.springframework; /** * @author 秦林森 */ public class Test { public static void main(String ...
- Fragment里使用 PhoneGap 的 CordovaWebView
首先说明一下为什么要使用 CordovaWebView 而不直接使用 WebView 呢?由于 Android4.4 版本之后对 WebView 做了很大的改变,具体参考 这篇文章:理解WebKit和 ...
- 关于hadoop: command not found的问题
问题: 昨天在安装完hadoop伪分布式之后,执行hadoop下的子项目--文字计数功能时出现该错误,然后今天执行 hadoop fs -ls命令时系统给出同样的错误提醒,经过查找资料,初步认为是ha ...
- 【poj3294-不小于k个字符串中最长公共子串】后缀数组
1.注意每两个串之间的连接符要不一样. 2.分组的时候要注意最后一组啊!又漏了! 3.开数组要考虑连接符的数量.100010是不够的至少要101000. #include<cstdio> ...
- bzoj 4999: This Problem Is Too Simple!
Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将i节点的值改为x. 2. Q i j x(0<=x&l ...
- hashlib模块加密用法
hashlib 加密模块 hashlib.md5() 构建一个md5的对象,用于调用对象的update方法去加密 例子: import hashlib hash = hashlib.md5() h ...
- Linux音频编程
1. 背景 在<Jasper语音助理介绍>中, 介绍了Linux音频系统, 本文主要介绍了Linux下音频编程相关内容. 音频编程主要包括播放(Playback)和录制(Record), ...