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
  1. 3
    1 2 3
output
  1. 3
    1 2 3
    1 2 3
input
  1. 3
    2 2 2
output
  1. 1
    1 1 1
    2
input
  1. 2
    2 1
output
  1. -1[]
  2.  
  3. 题意:f:[n]-->[m];g:[n]-->[m];h:[m]-->[n].给定nf[1--n]的值,求一个m,使得g[h[x]]==x,h[g[x]]==f[x]。
  4.  
  5. 比赛中看到这道题时,心里有点害怕,感觉做不出来,毕竟不太擅长这种题。然后第二天冷静了一下,AC掉。
  6.  
  7. 思路:
    g[h[x]]==xh[g[x]]==f[x]可知,只有当f[x]==xx(或f[x])值可以做h[1--m]的值域中的元素,
  1. 所以由f[x]==x可以确定h[x]和m的值;有了h[x],由h[g[x]]==f[x]可以确定g[x]。
  2.  
  3. 并且h[1--m]的值域和f[1--n]的值域应该完全相同,否则不可能。因为若f[1--n]的值域中存在一个I,但不在h[1--m]的值域中,那么就存在一个
  1. g[t]不能被确定。
  2.  
  3. 不可能的情况什么时候发生? 由以上思路,及由h[1--m]和f[1--n]若存在一个g[i]不能被确定,那么就是不可能的情况。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<map>
  5. using namespace std;
  6. #define N 100005
  7.  
  8. int f[N],g[N],h[N];
  9. map<int,int>maph;
  10. bool vis[N];
  11. int main()
  12. {
  13. int n,cntg;
  14. scanf("%d",&n);
  15. int m,last=;
  16. cntg=;
  17. for(int i=; i<=n; i++)
  18. {
  19. scanf("%d",&f[i]);
  20. if(f[i]==i)
  21. {
  22. vis[f[i]]=;
  23. }
  24. }
  25. //numg[cntg-1]+=n-last;
  26.  
  27. m=;
  28. for(int i=; i<=n; i++)
  29. if(vis[i]==)
  30. {
  31. m++;
  32. h[m]=i;
  33. maph[i]=m; //值域到定义域的映射
  34. }
  35. int flag=;
  36. for(int i=; i<=n; i++)
  37. {
  38. if(maph[f[i]]>)
  39. g[i]=maph[f[i]];
  40. else
  41. flag=;
  42. }
  43. if(flag)
  44. {
  45. printf("%d\n",m);
  46. for(int i=; i<=n; i++)
  47. {
  48. printf("%d",g[i]);
  49. if(i==n)
  50. printf("\n");
  51. else
  52. printf(" ");
  53. }
  54. for(int i=; i<=m; i++)
  55. {
  56. printf("%d",h[i]);
  57. if(i==m)
  58. printf("\n");
  59. else
  60. printf(" ");
  61. }
  62. }
  63. else
  64. printf("-1\n");
  65. return ;
  66. }
  1.  
  1.  
  1.  

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. android/java经常使用的工具类源代码

    anroid.java经常使用的工具类源代码,当中包含文件操作.MD5算法.文件操作.字符串操作.调试信息log.base64等等. 下载地址:http://download.csdn.net/det ...

  2. react 项目实战(八)图书管理与自动完成

    图书管理 src / pages / BookAdd.js   // 图书添加页 /** * 图书添加页面 */ import React from 'react'; // 布局组件 import H ...

  3. nodejs参考文章

    http://www.cnblogs.com/lily1010/p/6683987.html https://manlili.github.io/2015/04/06/Node%E5%85%A5%E9 ...

  4. PHP中的extract函数的用途 extract($_GET);extract($_POST)

    把客户端表单中的变量名取出来 addslashes -- 使用反斜线引用字符串 extract(addslashes($_POST)); --处理POST表单 把客户端<FORM METHOD= ...

  5. C++之new和malloc差别

         在C++程序猿面试中.非常easy被问到new 和 malloc的差别.偶尔在quora上逛.看到Robert Love的总结.才发现自己仅仅知道里面的一两项就沾沾自喜,从来没有像这位大牛一 ...

  6. Android学习笔记-listview实现方式之BaseAdapter

    listview是Android开发中最为常用的组件,这里我们就学习一下用BaseAdapter的方式实现listview, 主布局activity_main.xml是这样的: <LinearL ...

  7. flash、flex builder、flash builder、 air的关系

    flash VS flex builder flash被adobe收购的时候是flash8,已经可以AS2面向对象了. 而被adobe收购后,adobe准备把flash打造成一个开发工具.就比如JBU ...

  8. [Codeforces 466C] Number of Ways

    [题目链接] https://codeforces.com/contest/466/problem/C [算法] 维护序列前缀和 , 枚举中间一段即可 , 详见代码 时间复杂度 : O(N) [代码] ...

  9. Code First:Data Anotation (2)

    示例一 本例演示与索引有关的内容,模型: using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAn ...

  10. POJ1389 Area of Simple Polygons 线段树

    POJ1389 给定n个整数点矩形,求面积并. 显然ans必然是整数. 记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束. 进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了 ...