Codeforces Round #397 by Kaspersky Lab and Barcelona bootcamp (div. 1 + div. 2 combined) D. Artsem and Saunders

Source: Internet
Author: User

Address: Http://


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.


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 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.

1 2 3
1 2 3
1 2 3
2 2 2
1 1 1
2 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.