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. (工具类)Linux笔记之终端日志记录工具script

    在学习Linux时,有时候终端的打印消息对于我们很重要,可是终端显示也是有一定的缓冲空间的.当信息打印许多时,前面的信息就会被覆盖掉.所以这里网上搜索了一下这方面的介绍.现总结例如以下: script ...

  2. react 引入 百度地图API

    使用 Echarts 的地图的时候,发现报错,说 Bmap api is not loaded 百度地图API没有加载 乍一想,Echarts 用的也是 百度地图 啊,没有引入百度地图,还用个啥,当然 ...

  3. linux 点命令

    cat a date . a Mon Jun :: CST linux .(点命令):读取并且在当前的shell中执行文件中的命令

  4. YTU 2630: E2 驾驭const

    2630: E2 驾驭const 时间限制: 1 Sec  内存限制: 128 MB 提交: 673  解决: 491 题目描述 引入了const关键词,用于指定"常"对象及&qu ...

  5. CDN域名集合

    akamai-staging.net akamaized.net akamai.net cloudfront.net amazonaws.com azioncdn.net cachefly.net c ...

  6. vue 基础知识 随笔

    window.localStorage.gettItem("someItem"||[])//如果localStorage中的someItem不存在就返回一个空数组 window.l ...

  7. E20170528-ts

    partial   adj. 部分的; 偏爱的; 偏袒的; 钟爱的; form   n. 表格; 方式; 形状,形式; 外形 annotate   vt. 注解,注释; n. 注释者; annotat ...

  8. SQL 事务篇和约束

    数据库事务: 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行 事务是恢复和并发控制的基本单位.事务应该具有4个属性 ...

  9. 枚举+贪心 HDOJ 4932 Miaomiao's Geometry

    题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...

  10. BFS Codeforces Beta Round #94 (Div. 2 Only) C. Statues

    题目传送门 /* BFS:三维BFS,坐标再加上步数,能走一个点当这个地方在步数内不能落到.因为雕像最多8步就会全部下落, 只要撑过这个时间就能win,否则lose */ #include <c ...