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 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- 32个使用python代码片段
1.冒泡排序 lis = [56,12,1,8,354,10,100,34,56,7,23,456,234,-58] def sortport(): for i in range(len(lis ...
- C# 数值类型和无穷大
在c#语言中的数字有两个特性要了解.例如:任何数除以0所得的结果是无穷大,不在int long 和decimal类型的范围内.所以计算(一个数除以0会出错),但是在double和float类型中有一个 ...
- 2、在VM上的 CentOS 6.5 上安装mysql
1.查看系统是否安装了MySQL 使用命令: #rpm -qa | grep mysql 2.卸载已安装的MySQL 卸载mysql命令如下: #rpm -e - ...
- 导弹拦截 p1020
第一问就是求最长不上升子序列的长度,要写O(nlogn)的算法.... 对于这种nlogn的算法,只能求出长度,不能求出具体的序列.这种算法实现过程如下: 我们定义len为到目前为止最长不上升子序列的 ...
- Docker学习笔记之搭建 Java Web 项目运行环境
0x00 概述 Java Web 泛指以 Java 程序为基础向外提供 Web 服务的技术及相关工具,狭义上来说,我们也可以说 Java Web 是由 Servlet 程序提供的 Web 服务. 对我 ...
- Spring Boot 2 (八):Spring Boot 集成 Memcached
Spring Boot 2 (八):Spring Boot 集成 Memcached 一.Memcached 介绍 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数 ...
- udp编程 实例
server端 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <er ...
- direct加载之ora-39782一例
近日,我们有个环境在数据加载到oracle的时候出现ora-39782异常,版本是11.2.经google,几乎没有什么先例,因为我们是使用oci直接写的,可见现在还使用oci接口并不多,也或者我们的 ...
- The address where a.out.debug has been loaded is missing以及No symbol "*" in current context原因与解决方法
最近,在debug core的时候,发现p 变量的时候提示“No symbol "*" in current context”,我们的代码使用-g编译的,经查有可能是下列几个原因或 ...
- Bootstrap3基础 nav 便签页(横版、竖版)
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...