地址: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. “SYSTEM.DATA.SQLCLIENT.SQLCONNECTION”的类型初始值设定项引发异常---解决方案

    “System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常 问题出在了 .net 的C:\WINDOWS\Microsoft.NET\Framework\v2 ...

  2. word页眉与页脚详解

    1.如何隔离封面等不需要插入页码的页面: 首先插入分节符下一页(一定是分节符),再在下一页(即要开始插入页码的页面)选择视图-->页眉和页脚-->设置为取消链接到前一页.设置页码格式为起始 ...

  3. PhoneGap 获得设备属性Demo

    <!DOCTYPE html> <html> <head> <title>设备属性Demo</title> <script type= ...

  4. angular.js记录

    http://www.runoob.com/angularjs/angularjs-tutorial.html 第一部分:快速上手1.1 angularJS四大核心特性1.2 自己动手搭建开发,调试, ...

  5. php自定义函数: 下载本地服务器的大文件

    // 使用方法 $file_path = './a.zip'; // 只能是本地服务器文件, 多大的文件都支持!! down_file($file_path); // 函数参数: 服务器文件路径,下载 ...

  6. css中的clear:both,display:flex;

    介绍两者一起讨论的原因: 在明天就国庆的日子里陪着程序员的只有代码,啤酒,还有音乐,然后就是灯光下默默陪伴自己的影子.好了,不矫情了. -------------------------------- ...

  7. css3的3d属性集合

    css3的3d属性集合 想进入css3的3d世界必须知道一下几个属性及其用法. 当然在进入属性介绍之前我想你必须知道3维坐标,附上一张经典图如下: 我想不用解释了吧.之后要平移和旋转都是在这个基础之上 ...

  8. lombok插件使用

    1.1 lombok介绍 lombok 是一个可以帮助我们简化java代码编写的工具类,尤其是简化javabean的编写,可以通过采用注解的方式,消除代码中的构造方法,getter/setter等代码 ...

  9. Jmeter Aggregate Report 与 Summary Report 分析

    关于Aggregate Report和 Summary Report里面每个字段的说明,在网上有很多资料,在此不做说明. 本文主要讲Aggregate Report与Summary Report对比我 ...

  10. Python爬虫之-Requests

    Requests模块 Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了. 它是为另一个时代.另一个互联网所创建的.它需要巨量的 ...