地址:http://codeforces.com/contest/765/problem/D

题目:

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

思路:这题思路清晰的人应该可以很快做出来的。

  g(h(x)) = x ,可以推出m<=n;

  h(g(x))=f(x),可以推出m>=f(x)中不同元素的个数。

  先保证第二个条件h(g(x))=f(x):

    h[]=去重后的f[]。同时记录f[i]在h[]中的hash值:g[i]hash[f[i]]。

  然后扫一遍g[h[i]],判断值是否为x。

  我不知道怎么证明可以这么做,感觉可行后莽一发就ac了。

  有谁知道了可以交流下。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+; int n,m,ans;
int h[K],g[K],f[K];
map<int,int>hs; int main(void)
{
cin>>n;
for(int i=,x;i<=n;i++)
{
scanf("%d",&x),f[i]=x;
if(!hs[x]) h[++m]=x,hs[x]=m;
}
for(int i=;i<=n;i++)
g[i]=hs[f[i]];
for(int i=;i<=m;i++)
if(g[h[i]]!=i) ans=;
if(ans)
printf("-1\n");
else
{
printf("%d\n",m);
for(int i=;i<=n;i++)
printf("%d ",g[i]);
puts("");
for(int i=;i<=m;i++)
printf("%d ",h[i]);
puts("");
}
return ;
}

Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders的更多相关文章

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

  2. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

  3. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set

    F. Souvenirs 题目连接: http://codeforces.com/contest/765/problem/F Description Artsem is on vacation and ...

  4. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序

    E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...

  5. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C. Table Tennis Game 2 水题

    C. Table Tennis Game 2 题目连接: http://codeforces.com/contest/765/problem/C Description Misha and Vanya ...

  6. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B. Code obfuscation 水题

    B. Code obfuscation 题目连接: http://codeforces.com/contest/765/problem/B Description Kostya likes Codef ...

  7. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A. Neverending competitions 水题

    A. Neverending competitions 题目连接: http://codeforces.com/contest/765/problem/A Description There are ...

  8. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding

    地址:http://codeforces.com/contest/765/problem/E 题目: E. Tree Folding time limit per test 2 seconds mem ...

  9. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C - Table Tennis Game 2

    地址:http://codeforces.com/contest/765/problem/C 题目: C. Table Tennis Game 2 time limit per test 2 seco ...

随机推荐

  1. Tuning 04 Sizing the Buffer Cache

    Buffer Cache 特性 The buffer cache holds copies of the data blocks from the data files. Because the bu ...

  2. python 的简单抓取图片

    在我们日常上网浏览网页的时候,经常会看到一些好看的图片,我们就希望把这些图片保存下载,或者用户用来做桌面壁纸,或者用来做设计的素材. 我们最常规的做法就是通过鼠标右键,选择另存为.但有些图片鼠标右键的 ...

  3. 【BZOJ】3403: [Usaco2009 Open]Cow Line 直线上的牛(模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3404 裸的双端队列.. #include <cstdio> #include <c ...

  4. 该如何将MathType公式粘贴到文档中

    MathType是一款非常好用的数学公式编辑器,因为它与很多的文档处理器都能够很好地兼容,因此其使用范围非常的大.MathType公式编辑器编辑的公式与数学符号等都符号国际期刊的出版标准,并且上手容易 ...

  5. WPF DataGrid 获取选中的当前行某列值

    方法一: DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; ]ToString(); 方法二: var a = ...

  6. Android显示全局Dialog

    1.需求:Dialog只能依附于某个具体的Activity显示,当我们需要一个全局的不依赖于某个Activity的Dialog时,应该怎么创建?类似于QQ消息提醒框 2.分析:我们可以在Service ...

  7. 修改project任务的默认开始时间

    一般情况下,在project新建一个项目之后,在这个项目里每新添加一个任务,它的默认开始时间都是取的本项目的创建时间.但这可能并不是我们所需要的. 其实在这里,project取的默认开始时间都是整个项 ...

  8. [转]python-元类

    转载于:刘羽冲 两句话掌握python最难知识点——元类 千万不要被所谓“元类是99%的python程序员不会用到的特性”这类的说辞吓住.因为每个中国人,都是天生的元类使用者 学懂元类,你只需要知道两 ...

  9. Spring Cloud Zuul实现IP访问控制

    接着上篇文章 https://www.cnblogs.com/mxmbk/p/9569438.html IP访问限制和黑白名单如何做,需要解决以下几个问题: 1.如何识别正常访问和异常访问?(一段时间 ...

  10. lookcss在深夜23:32开通

    CSS在深夜23:32开通 话说 哥也耐不住寂寞搞个网站玩玩.顺便记录一些生活和学习和工作和思想和神马的点点滴滴~ 好吧 ~本来想起个名字叫涉趣.谁知道百度了一下,已经有人叫这个名字了,杯具,貌似还是 ...