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的更多相关文章

  1. 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 ...

  2. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  3. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  4. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  5. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Round #371 (Div. 1)

    A: 题目大意: 在一个multiset中要求支持3种操作: 1.增加一个数 2.删去一个数 3.给出一个01序列,问multiset中有多少这样的数,把它的十进制表示中的奇数改成1,偶数改成0后和给 ...

  9. Codeforces Round #268 (Div. 2) ABCD

    CF469 Codeforces Round #268 (Div. 2) http://codeforces.com/contest/469 开学了,时间少,水题就不写题解了,不水的题也不写这么详细了 ...

随机推荐

  1. Ubuntu 为 root 帐号开启 SSH 登录

    1. 修改 root 密码sudo passwd root 2. 以其他账户登录,通过 sudo nano 修改 /etc/ssh/sshd_config :xxx@ubuntu:~$ su - ro ...

  2. flask框架----上下文管理

    一.上下文管理相关知识点: a.类似于本地线程 创建Local类: { 线程或协程唯一标识: { 'stack':[request],'xxx':[session,] }, 线程或协程唯一标识: { ...

  3. Git 常用命令列表

    1 常用 $ git remote add origin git@github.com:yeszao/dofiler.git # 配置远程git版本库 $ git pull origin master ...

  4. mysql用户的增删与密码丢失问题

    为root用户设置初始密码 mysqladmin -u root password 密码(单实例) mysqladmin -u root password 密码 -S /data/3306/mysql ...

  5. javadoc中{@link}与@see的简单使用以及区别

    因为公司的测试直接对着java api进行外部服务的测试,故最近对javadoc的要求从0变成了严重依赖,有些时候在javadoc不得不引用其他class,以便更加清晰的说明上下文以及含义,发现指向别 ...

  6. php5.3.x连接MS SQL server2008

    开篇 因为毕设老师需求的原因,虚拟旅游网站要求的数据库必须使用MS SQL server. 我最擅长的web编程语言是PHP,但是在PHP中链接MS SQL server是一件非常麻烦的事,我个人分析 ...

  7. bzoj 2936 [Poi 1999] 降水 - 并查集

    题目传送门 需要root权限的传送门 题目大意 有一个$n\times m$的网格图,每一格都有一个高度.一次降雨过后问最多能积多少水. 考虑算每一高度能储存的水的量. 如果小于等于这个高度的格子和边 ...

  8. Install Virtualbox on ubuntu

    1.Use the command: sudo apt-get install virtualbox

  9. topcoder srm 350 div1

    problem1 link 满足$a^{b}\leq5000000,b>1$的数字不多,有2000多个,直接暴力计算能组成哪些数字即可. import java.util.*; import j ...

  10. topcoder srm 695 div1 -3

    1.称一个串的子串(连续一段)为$same$当且仅当这个子串所有字符都一样.子串不同当且仅当在原串中的起始位置不同.现在构造一个长度为$n$的只包含字符'a','b'的串$s$,使得$s$满足长度为$ ...