【题目链接】:http://codeforces.com/contest/765/problem/D

【题意】



给你一个函数f(x);

要让你求2个函数g[x]和h[x],使得g[h[x]] = x对1..m成立

h[g[x]] = f[x]对1..n成立;

这里

g[x]:[n]->[m];

h[x]:[m]->[n];

让你求h[x]的m的值,顺便输出两个函数的各个值

【题解】



这里要对f(x)做文章;

考虑f(x)==x的情况;

这时要想让

g[h[x]] = x对1..m成立

h[g[x]] = f[x]对1..n成立;

可以新加一个m;

h[++m] = x;

g[x] = m;

这样

g[h[m]] = m

h[g[x]] = x = f(x)

也成立了;

整个程序就以此为发生器;

求出了部分的g函数和全部的h函数(即只有f[x]==x的时候才能得出一个h函数);

之后对于那些还没有得到的g函数;

     rep1(i,1,n)
{
if (g[i]!=0) continue;
if (h[g[f[i]]] == f[i])
g[i] = g[f[i]];
else
return puts("-1"),0;
}

这里

因为

g[x]:[n]->[m];

h[x]:[m]->[n];

所以h[g[f[i]]]返回的也是一个1..n的值,f[i]也是在1..n;

如果

h[g[f[i]]] == f[i]成立的话;

显然可以让g[i]=g[f[i]];

因为这样

h[g[i]] = f[i]就成立了;

(上面的过程就相当于在构造一个g[i]出来让h[g[i]]=f[i]成立);

而对于g[h[i]]=i (i∈[1..m])这个条件则不用管它了;

因为所有的h数组已经全部知道了,在上面f[i]==i那个判定的时候就已经保证了

g[h[i]]=i是恒成立的了;

脑洞比较大。。想不出来

当时做的时候就想着那么多人AC了;

肯定挺简单的吧;

就随便让g[1]=1,然后以这个为发生器搞整个g和h;

还是太年轻了。



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e6+100; int n,m;
int f[N],g[N],h[N]; int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);
rep1(i,1,n)
{
rei(f[i]);
if (f[i]==i)
{
//g[h[i]] = i
//h[g[i]] = f[i]
h[++m] = i;
g[i] = m;
}
}
rep1(i,1,n)
{
if (g[i]!=0) continue;
if (h[g[f[i]]] == f[i])
g[i] = g[f[i]];
else
return puts("-1"),0;
}
printf("%d\n",m);
rep1(i,1,n)
{
printf("%d",g[i]);
if (i==n)
puts("");
else
putchar(' ');
}
rep1(i,1,m)
{
printf("%d",h[i]);
if (i==m)
puts("");
else
putchar(' ');
}
return 0;
}

【codeforces 765D】Artsem and Saunders的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  3. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  4. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  5. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  6. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  7. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  8. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

  9. 【codeforces 515D】Drazil and Tiles

    [题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...

随机推荐

  1. 106.TCP传文件

    客户端 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include < ...

  2. 获取input file 选中的图片,并在一个div的img里面赋值src实现预览

    代码如下利用html5实现:几乎兼容所有主流浏览器,当然IE必须是IE 6以上 [jquery代码] $(function() { $("#file_upload").change ...

  3. “-bash: !”: event not found"、echo > sudo permission denied

    1. "-bash: !": event not found" 比如当我们在 linux 命令行输入echo "Reboot your instance!&qu ...

  4. mybatis-generator使用

    开发工具:eclipse,依赖环境 :maven 首先在eclipse marketplace中安装mybatis-generator,如图: 安装后需要重启. 新建一个maven项目MybatisG ...

  5. Springboot + shiro 整合之Url拦截设置(转)

    shiro 整合到springboot 还是比较简单的,只需要新建一个spring-shiro.xml的配置文件: <span style="font-size:14px;" ...

  6. java回调函数这样说,应该明确了吧!

    有哥们问我回调怎么用,回调怎么理解? 怎么说好呢,仅仅可意会不可言传呐,非也,回调在实际开发中使用频率事实上是非常高的,恰好我小时候也被回调函数欺负过,居然问了,那么肯定要好好分享一下我的一些经验. ...

  7. [Angular2 Router] Auxiliary Routes bit by bit

    Article Github Auxiliary Routes is is little bit hard to understand. Here is demo, link You can see ...

  8. Nginx和Nginx+的比較(下)

    Nginx和Nginx+的比較(下) 作者:chszs.未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 内容紧接上一篇<Nginx和 ...

  9. 【2047】求前n个完全数

    Time Limit: 10 second Memory Limit: 2 MB 问题描述 完全数又称完数.完美数.完备数,是一些特殊的自然数,它所有真因子(即除自己以外的因子)的和等于它本身.例如: ...

  10. Java中使用org.json和json-lib解析JSON

    文章目录  [隐藏] 一.JavaProject中org.json解析JSON 1.JSON的org.son-api下载 1)JSON网址 2)JSON的java解析org.json-api网址 3) ...