Codeforces Round #397 Div. 2 D. Artsem and Saunders
http://codeforces.com/problemset/problem/765/D
题意:
有一个函数f,f: [n] → [n] 的意思就是定义域为【1,n】,每个x值对应于【1,n】内的一个值(后面的意思也是如此)。现在确定是否存在这样的一个m以及函数h和g,使得满足上述要求。
思路:
对于h(g(x)) = f(x) ,因为x属于【1,n】,所欲f函数的所有值h函数都要包括,既然如此的话,那么f函数中有多少个不同的值,那么m的值就是多大,只需要让h(1),h(2)...依次等于这些值即可(再多其实也已经没有意义了)。
确定了h函数后,我们也就能够推出g函数了,只需要让f函数中的值做个映射即可,具体可以参见代码。确定了h和g函数之后,再用第二个式子判断一下是否满足要求。
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;
const int maxn = 1e6+; map<int,int> mp; int a[maxn],g[maxn],h[maxn]; int main()
{
//freopen("in.txt","r",stdin);
int n;
scanf("%d",&n);
int tot = ;
mp.clear();
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(mp[a[i]]==)
{
mp[a[i]] = ++tot;
h[tot] = a[i];
}
}
for(int i=;i<=n;i++) g[i] = mp[a[i]];
bool flag = true;
for(int i=;i<=tot;i++)
{
if(g[h[i]]!=i) {flag = false;break;}
}
if(!flag) puts("-1");
else
{
printf("%d\n",tot);
for(int i=;i<=n;i++) printf("%d%c",g[i],i!=n?' ':'\n');
for(int i=;i<=tot;i++) printf("%d%c",h[i],i!=tot?' ':'\n');
}
return ;
}
Codeforces Round #397 Div. 2 D. Artsem and Saunders的更多相关文章
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
- Codeforces Round #371 (Div. 1)
A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...
- Codeforces Round #268 (Div. 2) ABCD
CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- 20165215 2017-2018-2《Java程序设计》第一周学习总结
20165215 2017-2018-2 <Java程序设计>第一周学习总结 教材学习内容总结 跟随网课学习了<Java2 实用教程>的第一章,进行了基础的编译练习 在Ubun ...
- html5水平方向重力感应
html5图片随手机重力感应而移动 <!DOCTYPE html> <html lang="zh-cn"><head><meta http ...
- poj1185 [NOI2001炮兵阵地]
题目链接 状压DP 本来如果考虑所有情况应该开hh[n][2^10][2^10]表示i行在i-1的状态为j,i-2的状态为k的最大个数 但是由于每行中的人互相限制所以在m=10时只有60种情况 空间就 ...
- Prometheus监控学习笔记之全面学习Prometheus
0x00 概述 Prometheus是继Kubernetes后第2个正式加入CNCF基金会的项目,容器和云原生领域事实的监控标准解决方案.在这次分享将从Prometheus的基础说起,学习和了解Pro ...
- 创建docker镜像的私有仓库
CentOS Linux release 7.2.1511 Docker version 17.03.1-ce 安装registry镜像 同时安装一个比较小的镜像alpine待会作测试用: # doc ...
- Tomcat 7服务器线程模型
Tomcat 7服务器网络处理主要由NioEndpoint,其处理客户端连接的主要流程如图所示图中Acceptor及Worker分别是以线程池形式存在,Poller是一个单线程.注意,与BIO的实现一 ...
- .net下安装 ZooKeeper.Net
PM> Install-Package ZooKeeper.Net正在尝试解析依赖项“log4net (≥ 1.2.10)”.正在安装“log4net 1.2.10”.已成功安装“log4net ...
- python简说(四)字符串
s='.abc.abc.BCD,abc'# new_s = s.strip('.') #默认去掉字符串两边的空格和换行符# print(s.rstrip()) #去掉右边的# print(s.lstr ...
- web site optimization
@ 如果有很多图片(比如web服务器的页面上有多个小图片),通常是没有必要记录文件的访问时间的,这样就可以减少写磁盘的I/O,这个要如何配置 @ 首先,修改文件系统的配置文件/etc/fstab ,然 ...
- [内核驱动] DOS路径转化为NT路径
转载:http://blog.csdn.net/qq_33504040/article/details/78468278 最近在做一个文件过滤驱动程序,禁止访问指定目录或文件.想要从R3给R0发命令和 ...