国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

【Codeforces 756 D. Artsem and Saunders】+ 思維 + 構造

2019-11-08 20:15:31
字體:
來源:轉載
供稿:網友

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

給出數組 a,是否可以構造出數組g,h滿足 :g(h(x))?=?x ,h(g(x))?=?f(x) 數組h不重復記錄數組a里的元素,數組g(x)記錄a[x]在h數組里的位置,但若a[x] != x,便無法構成

AC代碼:

#include<cstdio>#include<algorithm>using namespace std;const int K = 1e5 + 10;int a[K],b[K],c[K],ok[K];int main(){ int N; scanf("%d",&N); for(int i = 1; i <= N; i++) scanf("%d",&a[i]),ok[a[i]] = 1; int nl = 0; for(int i = K - 1; i >= 1; i--) if(ok[i]){ c[++nl] = i,b[i] = nl; if(a[i] != i){printf("-1/n"); return 0;} } printf("%d/n",nl); for(int i = 1; i <= N; i++) printf("%d ",b[a[i]]);printf("/n"); for(int i = 1; i <= nl; i++) printf("%d ",c[i]); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奎屯市| 诸城市| 莱西市| 开封市| 泸州市| 双柏县| 平度市| 滦平县| 柳河县| 拉孜县| 清徐县| 忻城县| 嘉定区| 开平市| 韶山市| 申扎县| 威信县| 高邮市| 娄烦县| 文安县| 永济市| 肇东市| 泽州县| 新密市| 定边县| 军事| 基隆市| 梧州市| 庆安县| 尼木县| 修水县| 陆河县| 淮南市| 五家渠市| 锡林郭勒盟| 中卫市| 遂昌县| 合川市| 肇庆市| 浦县| 西宁市|