Codeforces_765_D. Artsem and Saunders_(数学)
2 seconds
512 megabytes
standard input
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.
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).
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.
- 3
1 2 3
- 3
1 2 3
1 2 3
- 3
2 2 2
- 1
1 1 1
2
- 2
2 1
- -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_(数学)的更多相关文章
- 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 ...
- 数学思想:为何我们把 x²读作x平方
要弄清楚这个问题,我们得先认识一个人.古希腊大数学家 欧多克索斯,其在整个古代仅次于阿基米德,是一位天文学家.医生.几何学家.立法家和地理学家. 为何我们把 x²读作x平方呢? 古希腊时代,越来越多的 ...
- 速算1/Sqrt(x)背后的数学原理
概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...
- MarkDown+LaTex 数学内容编辑样例收集
$\color{green}{MarkDown+LaTex 数学内容编辑样例收集}$ 1.大小标题的居中,大小,颜色 [例1] $\color{Blue}{一元二次方程根的分布}$ $\color{R ...
- 深度学习笔记——PCA原理与数学推倒详解
PCA目的:这里举个例子,如果假设我有m个点,{x(1),...,x(m)},那么我要将它们存在我的内存中,或者要对着m个点进行一次机器学习,但是这m个点的维度太大了,如果要进行机器学习的话参数太多, ...
- Sql Server函数全解<二>数学函数
阅读目录 1.绝对值函数ABS(x)和返回圆周率的函数PI() 2.平方根函数SQRT(x) 3.获取随机函数的函数RAND()和RAND(x) 4.四舍五入函数ROUND(x,y) 5.符号函数SI ...
- *HDU 2451 数学
Simple Addition Expression Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- 如何解决Maple的应用在数学中
对任意数学和技术学科的研究员.教师和学生而言,Maple是一个必备的工具.通过Maple,教师将复杂数学问题注入生命,学生的精力集中在概念理解上而不是如何使用工具上,研究员可以开发更复杂的算法或模型. ...
- 如何让Maple中的数学引擎进入你的桌面应用程序和网站
MapleNET数学服务套件将Maple 2015强大的数学引擎引入您的应用程序和网站.使用MapleNET,您可以添加数学计算和可视化功能到网页和桌面程序中,通过互联网/局域网分享“活”的Maple ...
随机推荐
- android/java经常使用的工具类源代码
anroid.java经常使用的工具类源代码,当中包含文件操作.MD5算法.文件操作.字符串操作.调试信息log.base64等等. 下载地址:http://download.csdn.net/det ...
- react 项目实战(八)图书管理与自动完成
图书管理 src / pages / BookAdd.js // 图书添加页 /** * 图书添加页面 */ import React from 'react'; // 布局组件 import H ...
- nodejs参考文章
http://www.cnblogs.com/lily1010/p/6683987.html https://manlili.github.io/2015/04/06/Node%E5%85%A5%E9 ...
- PHP中的extract函数的用途 extract($_GET);extract($_POST)
把客户端表单中的变量名取出来 addslashes -- 使用反斜线引用字符串 extract(addslashes($_POST)); --处理POST表单 把客户端<FORM METHOD= ...
- C++之new和malloc差别
在C++程序猿面试中.非常easy被问到new 和 malloc的差别.偶尔在quora上逛.看到Robert Love的总结.才发现自己仅仅知道里面的一两项就沾沾自喜,从来没有像这位大牛一 ...
- Android学习笔记-listview实现方式之BaseAdapter
listview是Android开发中最为常用的组件,这里我们就学习一下用BaseAdapter的方式实现listview, 主布局activity_main.xml是这样的: <LinearL ...
- flash、flex builder、flash builder、 air的关系
flash VS flex builder flash被adobe收购的时候是flash8,已经可以AS2面向对象了. 而被adobe收购后,adobe准备把flash打造成一个开发工具.就比如JBU ...
- [Codeforces 466C] Number of Ways
[题目链接] https://codeforces.com/contest/466/problem/C [算法] 维护序列前缀和 , 枚举中间一段即可 , 详见代码 时间复杂度 : O(N) [代码] ...
- Code First:Data Anotation (2)
示例一 本例演示与索引有关的内容,模型: using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAn ...
- POJ1389 Area of Simple Polygons 线段树
POJ1389 给定n个整数点矩形,求面积并. 显然ans必然是整数. 记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束. 进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了 ...