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 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- WEB前端移动开发初始化
meta篇 1.视窗宽度 <meta name="viewport" content="width=device-width,initial-scale=1.0,m ...
- java 数组和集合
1.概念说明 区别:数组固定长度的,集合,数组的长度是可以变化的. List,继承Collection,可重复.有序的对象 Set,继承Collection,不可重复.无序的对象 Map,键值对,提供 ...
- Kali linux vim使用命令笔记
Kali Linux系统的vi编辑器/vim编辑器的使用和CentOS有很多不同.基本使用方法如下 1.vi的基本概念 基本上vi可以分为三种状态,分别是命令模式(command mode).插入模式 ...
- EditPlus配置Java
--Java Compile-- 命令:D:\Program Files\Java\jdk1.7.0_79\bin\javac.exe 参数:$(FileName) 初始目录:$(FileDir) 动 ...
- JDBC和servlet设计思路、DAO模式思路、MVC思路粗略总结
#JDBC和Servlet联合起来使用的项目思路: 说明:建库,最好一开始设置utf8字符集 step1: 在数据库中建表 如 create table t_user{ ...... } step ...
- MySQL5.7 忘记root密码,怎么破?
MySQL5.7 忘记root密码,怎么破? 关服 # kill $mysql_pid 免密启动 # /usr/local/mysql57/bin/mysqld_safe --defaults-fil ...
- Java操作Solr之SolrJ
添加SolrJ的jar包 solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务, <depende ...
- Spring Boot 2 (四):使用 Docker 部署 Spring Boot
Spring Boot 2 (四):使用 Docker 部署 Spring Boot Docker 技术发展为微服务落地提供了更加便利的环境,使用 Docker 部署 Spring Boot 其实非常 ...
- 爬虫的基本操作 requests / BeautifulSoup 的使用
爬虫的基本操作 爬虫基础知识 什么是爬虫? 在最开始,还没有诞生Google和百度等一系列搜索引擎的公司的时候,人们进入一些公司的网站只能通过在浏览器地址栏输入网址的方式访问,如同在很早之前前手机不流 ...
- Android之xml解析
利用类下载器解析Xml文件要解析的xml文件<?xml version="1.0" encoding="utf-8"?><info> & ...