Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders
地址:http://codeforces.com/contest/765/problem/D
题目:
2 seconds
512 megabytes
standard input
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.
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 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.
3
1 2 3
3
1 2 3
1 2 3
3
2 2 2
1
1 1 1
2
2
2 1
-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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- php+mysqli实现批量执行插入、更新及删除数据的方法
本文实例讲述了php+mysqli实现批量执行插入.更新及删除数据的方法.分享给大家供大家参考.具体如下: mysqli批量执行插入/更新/删除数据,函数为 multi_query(). 下面的代码只 ...
- cocos2d安卓自动编译脚本去掉复制Resources资源和签名功能
去掉这两个功能的原因: 1.因为有时候打包是分渠道的,不同的渠道资源也可能不一样,所以不能直接复制资源. 2.如果用cocostudio打release包,因为要输入签名地址,会导致在自动签名处停住不 ...
- angularJS中的ng-show、ng-if指令
angularJS中的ng-show.ng-hide.ng-if指令都可以用来控制dom元素的显示或隐藏. 1. ng-show和ng-hide 根据所给表达式的值来显示或隐藏HTML元素.元素会渲染 ...
- 支付宝app支付提示 系统繁忙,请稍后重试
v2版本的支付宝支付,无论怎么调试返回的都是系统繁忙,请稍后重试提示.经过对给的示例代码的仔细研究,最后发现是由于老版本的sign_type 不加入签名计算,而新版本的支付签名它是加入签名计算的.供大 ...
- C# .net 数组倒序排序
1.数组方法 Array.Sort(Array Array); 此方法为数组的排序(正序)方法 Array.Reverse(Array Array); 此方法可以将数组中的值颠倒 两个方法结合使用 ...
- CodeForces 156B Suspects(枚举)
B. Suspects time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- DOM 编程入门
DOM (Document Object Model) 文档对象模型 文档: 标记型文档(具有标签, 属性以及标签中封装的数据) 对象: 封装了属性和行为的实例, 可以被直接调用 模型: 所有标记型文 ...
- Mysql实现企业级日志管理、备份与恢复
数据备份形式 文件备份: 通过Linux的备份命令把文件统一打个包存起来,可存在本地和远程服务器,等到要恢复时,再用这些文件恢复到指定位置. 数据库数据备份: 在一些对数据可靠性要求很高的行业如银行. ...
- 【零基础学习iOS开发】【02-C语言】08-基本运算
一.算术运算符 算术运算符很地简单.就是小学数学里面的一些加减乘除操作.只是呢.还是有一些语法细节须要注意的. 1.加法运算符 + 1 int a = 10; 2 3 int b = a + 5; 在 ...
- Android自定义属性:attr.xml 与 TypedArray
1.attr.xml <?xml version="1.0" encoding="utf-8"?> <resources> <de ...