【CodeForces 765D】 Artsem and Saunders(数学,构造)
来源:互联网 发布:怎么使用淘宝客 编辑:程序博客网 时间:2024/06/07 15:27
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 functionf is defined in integer points 1, ..., x, and all its values are integers from 1 toy.
Now then, you are given a function f: [n] → [n]. Your task is to find a positive integerm, and two functions g: [n] → [m], h: [m] → [n], such thatg(h(x)) = x for all, andh(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 — valuesf(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 printn numbers g(1), ..., g(n). On the third line printm 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.
31 2 3
31 2 31 2 3
32 2 2
11 1 12
22 1
#include <bits/stdc++.h>#define manx 100005using namespace std;int main(){ int f[manx]={0},h[manx]={0},g[manx]={0},n,m; scanf("%d",&n); for (int i=1; i<=n; i++) scanf("%d",&f[i]); int cot=1; for (int i=1; i<=n; i++){ if (g[f[i]]) g[i]=g[f[i]]; //g[f[i]]在g[i]之前 else if (g[i]==0) g[i]=cot++; if(!g[f[i]]) g[f[i]]=g[i]; //g[f[i]]在g[i]之后 else if (g[f[i]]!=g[i]){ printf("-1\n"); return 0; } } m=0; for (int i=1; i<=n; i++){ if(!h[g[i]]) { h[g[i]]=f[i]; m++; } else if(h[g[i]]!=f[i]){ printf("-1\n"); return 0; } } printf("%d\n",m); for (int i=1; i<=n; i++) i==n ? printf("%d\n",g[i]) : printf("%d ",g[i]); for (int i=1; i<=m; i++) i==m ? printf("%d\n",h[i]) : printf("%d ",h[i]); return 0;}
- 【CodeForces 765D】 Artsem and Saunders(数学,构造)
- Codeforces 765D Artsem and Saunders 构造
- Codeforces 765D Artsem and Saunders 【构造】
- Codeforces 765D Artsem and Saunders(数学)
- codeforces 765 D Artsem and Saunders(构造)
- codeforces #397 D. Artsem and Saunders(构造)
- Codeforces Round #397 D. Artsem and Saunders(构造)
- 【codeforces 765D】Artsem and Saunders
- codeforces 765D. Artsem and Saunders
- 【Codeforces 756 D. Artsem and Saunders】+ 思维 + 构造
- D. Artsem and Saunders----数学思维
- codeforces 397div2 D Artsem and Saunders
- codeforces 397div2 D Artsem and Saunders
- codeforces 397div2 D Artsem and Saunders
- codeforces 397div2 D Artsem and Saunders
- D. Artsem and Saunders
- D. Artsem and Saunders
- D. Artsem and Saunders
- 15-CoreData删除所有数据之NSBatchDeleteRequest
- 2016蓝桥杯C/C++省赛 交换瓶子(超级大水题)
- Maven profile
- BZOJ1212: [HNOI2004]L语言 AC自动机
- 【笔试真题】2017年58春招笔试题
- 【CodeForces 765D】 Artsem and Saunders(数学,构造)
- Linux下创建,进入目录的权限,创建并显示普通文件的权限
- JZOJ 1533. 郁闷的gxg
- linux 键盘输入错乱
- android opengl es 3d 贴图 旋转立方体demo
- 对Java.io中一些类的归纳,层次结构图
- 设置eclipse行号
- 细数云计算产品和技术-CloudStack
- 手机数据线这些小细节竟然会影响充电速度!