Address: Http://codeforces.com/contest/765/problem/D
Topic:
D. Artsem and SaundersTime limit per test2 secondsmemory limit per testMegabytesinputStandard InputOutputStandard 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'll also write f:? [ x]? →? [y] When a function f are defined in integer points 1, ..., x, and all its VA Lues is integers from 1 to y.
Now and then, you are given a function f:? [ N]? →? [N]. Your task is to find a positive integer m, and both 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 this 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 mnumbers h(1),?...,? H(m).
If There is several correct answers, you could output any of the them. It is guaranteed that if a valid answer exists and then there was an answer satisfying the above restrictions.
Examplesinput
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
Idea: The person with clear thinking should be able to do it quickly.
g (H(x))? =? x , can be launched m<=n;
H (g (x)) =f (x), the number of different elements in m>=f (x) can be introduced.
First, the second condition H (g (x)) =f (x) is guaranteed:
H[]= f[]. Also record the hash value of f[i] in h[]: g[i]hash[f[i].
Then sweep G[h[i]] to determine if the value is x.
I don't know how to prove that I can do this, and I feel that it is possible to get AC after a rash.
Who knows, can communicate.
1#include <bits/stdc++.h>2 3 using namespacestd;4 5 #defineMP Make_pair6 #definePB push_back7typedefLong LongLL;8typedef pair<int,int>PII;9 Const Doubleeps=1e-8;Ten Const DoublePi=acos (-1.0); One Const intk=1e5+7; A Const intmod=1e9+7; - - intN,m,ans; the intH[k],g[k],f[k]; -map<int,int>HS; - - intMainvoid) + { -Cin>>N; + for(intI=1, x;i<=n;i++) A { atscanf"%d", &x), f[i]=x; - if(!hs[x]) h[++m]=x,hs[x]=m; - } - for(intI=1; i<=n;i++) -g[i]=Hs[f[i]]; - for(intI=1; i<=m;i++) in if(g[h[i]]!=i) ans=1; - if(ANS) toprintf"-1\n"); + Else - { theprintf"%d\n", m); * for(intI=1; i<=n;i++) $printf"%d", G[i]);Panax NotoginsengPuts""); - for(intI=1; i<=m;i++) theprintf"%d", H[i]); +Puts""); A } the return 0; +}
Codeforces Round #397 by Kaspersky Lab and Barcelona bootcamp (div. 1 + div. 2 combined) D. Artsem and Saunders