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 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...
随机推荐
- Linux服务器---流量监控MRTG
MRTG MRTG可以分析网络流量,但是它必须依赖SNMP协议.将收集到的数据生成HTML文件,以图片的形式展示出来 1.安装一些依赖软件 [root@localhost bandwidthd-2.0 ...
- 转:【专题十二】实现一个简单的FTP服务器
引言: 休息一个国庆节后好久没有更新文章了,主要是刚开始休息完心态还没有调整过来的, 现在差不多进入状态了, 所以继续和大家分享下网络编程的知识,在本专题中将和大家分享如何自己实现一个简单的FTP服务 ...
- 文字列大好きいろはちゃんイージー / Iroha Loves Strings (ABC Edition) (优先队列)
题目链接:http://abc042.contest.atcoder.jp/tasks/abc042_b Time limit : 2sec / Memory limit : 256MB Score ...
- [转载] 关于出现“使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式”错误的可能原因
1. 对于该问题确实存在UNION前后SELECT语句中的列数不同导致:2. 以下为个人遇到的一种可能:在项目开发中由于有张表是动态的,即有个基础表,其他的表按年月根据基础表来生成动态表,动态表结构和 ...
- 设置PyCharm创建文件时自动添加头文件
找到该路径并添加以下信息 File->settings->Editor->File and Code Templates->Python Script #!/usr/bin/ ...
- How to use Nissan consult 3 plus to check, make key and program?
How to use Nissan consult 3 plus to test Nissan? Firstly: get one particular Nissan consult 3 plus. ...
- Lonsdor K518ISE free update news on what makes and year can work
Lonsdor K518ISE engineers recently tested a number of cars and verified working great, below are tes ...
- Python爬虫【二】请求库requests
一.requests的常用请求方式 #各种请求方式:常用的就是requests.get()和requests.post() >>> import requests >>& ...
- Zynq ZC706 传统方式移植Linux -- 编译u-boot
我用的是zc706不是zed 基本思路是: 1.安装交叉编译工具(见 https://www.cnblogs.com/idyllcheung/p/10532654.html ) 2.下载xilinx ...
- Solr导入MySQL数据之dataimport-handler
Solr不借助手动JSolr编程情况下也可以将Mysql的数据导入到Solr中.实现方式是安装dataimport-Handler从关系数据库将数据导入到索引库. 1.向SolrCore中加入jar包 ...