D. Artsem and Saunders
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Artsem has a friend Saunders from University of Chicago. Saunders presented him with the following problem.

Let [n] denote the set {1, ..., n}. We will also write f: [x] → [y] when a function f is defined in integer points 1, ..., x, and all its values are integers from 1 to y.

Now then, you are given a function f: [n] → [n]. Your task is to find a positive integer m, and two functions g: [n] → [m], h: [m] → [n], such that g(h(x)) = x for all , and h(g(x)) = f(x) for all , or determine that finding these is impossible.

Input

The first line contains an integer n (1 ≤ n ≤ 105).

The second line contains n space-separated integers — values f(1), ..., f(n) (1 ≤ f(i) ≤ n).

Output

If there is no answer, print one integer -1.

Otherwise, on the first line print the number m (1 ≤ m ≤ 106). On the second line print n numbers g(1), ..., g(n). On the third line print m numbers h(1), ..., h(m).

If there are several correct answers, you may output any of them. It is guaranteed that if a valid answer exists, then there is an answer satisfying the above restrictions.

Examples
input
3
1 2 3
output
3
1 2 3
1 2 3
input
3
2 2 2
output
1
1 1 1
2
input
2
2 1
output
-1[]

题意:f:[n]-->[m];g:[n]-->[m];h:[m]-->[n].给定n和f[1--n]的值,求一个m,使得g[h[x]]==x,h[g[x]]==f[x]。

比赛中看到这道题时,心里有点害怕,感觉做不出来,毕竟不太擅长这种题。然后第二天冷静了一下,AC掉。

思路:
由g[h[x]]==x和h[g[x]]==f[x]可知,只有当f[x]==x的x(或f[x])值可以做h[1--m]的值域中的元素,
所以由f[x]==x可以确定h[x]和m的值;有了h[x],由h[g[x]]==f[x]可以确定g[x]。

并且h[1--m]的值域和f[1--n]的值域应该完全相同,否则不可能。因为若f[1--n]的值域中存在一个I,但不在h[1--m]的值域中,那么就存在一个
g[t]不能被确定。

不可能的情况什么时候发生? 由以上思路,及由h[1--m]和f[1--n]若存在一个g[i]不能被确定,那么就是不可能的情况。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
#define N 100005 int f[N],g[N],h[N];
map<int,int>maph;
bool vis[N];
int main()
{
int n,cntg;
scanf("%d",&n);
int m,last=;
cntg=;
for(int i=; i<=n; i++)
{
scanf("%d",&f[i]);
if(f[i]==i)
{
vis[f[i]]=;
}
}
//numg[cntg-1]+=n-last; m=;
for(int i=; i<=n; i++)
if(vis[i]==)
{
m++;
h[m]=i;
maph[i]=m; //值域到定义域的映射
}
int flag=;
for(int i=; i<=n; i++)
{
if(maph[f[i]]>)
g[i]=maph[f[i]];
else
flag=;
}
if(flag)
{
printf("%d\n",m);
for(int i=; i<=n; i++)
{
printf("%d",g[i]);
if(i==n)
printf("\n");
else
printf(" ");
}
for(int i=; i<=m; i++)
{
printf("%d",h[i]);
if(i==m)
printf("\n");
else
printf(" ");
}
}
else
printf("-1\n");
return ;
}



Codeforces_765_D. Artsem and Saunders_(数学)的更多相关文章

  1. 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 ...

  2. 数学思想:为何我们把 x²读作x平方

    要弄清楚这个问题,我们得先认识一个人.古希腊大数学家 欧多克索斯,其在整个古代仅次于阿基米德,是一位天文学家.医生.几何学家.立法家和地理学家. 为何我们把 x²读作x平方呢? 古希腊时代,越来越多的 ...

  3. 速算1/Sqrt(x)背后的数学原理

    概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...

  4. MarkDown+LaTex 数学内容编辑样例收集

    $\color{green}{MarkDown+LaTex 数学内容编辑样例收集}$ 1.大小标题的居中,大小,颜色 [例1] $\color{Blue}{一元二次方程根的分布}$ $\color{R ...

  5. 深度学习笔记——PCA原理与数学推倒详解

    PCA目的:这里举个例子,如果假设我有m个点,{x(1),...,x(m)},那么我要将它们存在我的内存中,或者要对着m个点进行一次机器学习,但是这m个点的维度太大了,如果要进行机器学习的话参数太多, ...

  6. Sql Server函数全解<二>数学函数

    阅读目录 1.绝对值函数ABS(x)和返回圆周率的函数PI() 2.平方根函数SQRT(x) 3.获取随机函数的函数RAND()和RAND(x) 4.四舍五入函数ROUND(x,y) 5.符号函数SI ...

  7. *HDU 2451 数学

    Simple Addition Expression Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. 如何解决Maple的应用在数学中

    对任意数学和技术学科的研究员.教师和学生而言,Maple是一个必备的工具.通过Maple,教师将复杂数学问题注入生命,学生的精力集中在概念理解上而不是如何使用工具上,研究员可以开发更复杂的算法或模型. ...

  9. 如何让Maple中的数学引擎进入你的桌面应用程序和网站

    MapleNET数学服务套件将Maple 2015强大的数学引擎引入您的应用程序和网站.使用MapleNET,您可以添加数学计算和可视化功能到网页和桌面程序中,通过互联网/局域网分享“活”的Maple ...

随机推荐

  1. HDU 5319 Painter(枚举)

    Painter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su ...

  2. ZOJ问题(2010浙江大学研究生复试上机题目[找规律] hdoj 3788)

    ZOJ问题 pid=3788">点击打开链接 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  3. ubuntu下apache+mysql+php+mysql等之webserver搭建

    相信非常多人跟我一样,想搭建一个自己的webserver.网上资料非常多.可是因为版本号的区别,总是存在依照一个教程来做无法全然实现的问题.近期我也折腾了好几天,google不能用,仅仅能百度,真想说 ...

  4. Windows命令实现匿名邮件发送

    在日常工具开发中,常常会有发送邮件的需求.在一些高级语言中,如Python.C#中,都有专门的邮件发送模块,如Python 中的 smtplib 模块.那么.一封邮件究竟是怎样发送到一个特定的邮箱呢? ...

  5. C#项目的生成事件及批处理文件

    一个C#项目,如果为同一个解决方案的其他项目所引用,则其编译后,会将DLL拷贝到引用项目中:但如果它并不被其他项目引用,但又想编译后能够自动将生成的东西拷贝过去,可以在项目的生成事件中,写上一些批处理 ...

  6. JFreeChart生成饼形图(3)11 (转自 JSP开发技术大全)

    FreeChart生成饼形图(3) (转自 JSP开发技术大全) 14.3 利用JFreeChart生成饼形图 通过JFreeChart插件,即可以生成普通效果的饼形图,也可以生成3D效果的饼形图:如 ...

  7. [LeetCode] LRU Cache [Forward]

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  8. python-----文件自动归类

    如何移动文件? →  使用内置模块来实现 归类的规则是什么? → 手动(预设文件夹)/ 自动(创建文件夹) import shutil import os path = './' #由于这里是相对路径 ...

  9. JSP-Runoob:JSP 结构

    ylbtech-JSP-Runoob:JSP 结构 1.返回顶部 1. JSP 结构 网络服务器需要一个 JSP 引擎,也就是一个容器来处理 JSP 页面.容器负责截获对 JSP 页面的请求.本教程使 ...

  10. VS2015 framework4.5代码提示英文切换为中文

    输入下面的地址,复制里面所有的文件 C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0 ...