一个变换题

给定f(x),[1,n]->[1,n]

构造g(x),h(x)满足:

g(h(x))=x [1,n]->[1,m]

h(g(x))=f(x) [1,m]->[1,n]

根据已知条件,等效替换变形:

h(g(h(x)))=f(h(x))

=>h(x)=f(h(x))

=>h(g(x))=f(h(g(x)))

=>f(x)=f(f(x)) //是不是有点类似并查集找爸爸

另一方面:

g(h(g(x)))=g(x)

=>g(x)=g(f(x))

这里我们又可以发现f,g,h三个函数的值域其实都是[1,m],等价于将n个元素分为m类

那么题目意思是不是可以转化成:

f(i):找i这个元素的爸爸元素是谁

g(i):找元素i属于哪一类

h(i):找第i类的爸爸元素是谁

这不就是很经典的(排名<<=>>权值)互换函数么

int n, m;
int f[N], g[N], h[N];
int vis[N]; int main()
{
#ifndef ONLINE_JUDGE
file("test");
#endif
sdf(n);
For(i, 1, n) sdf(f[i]);
For(i, 1, n)
{
if (f[f[i]] != f[i])
{
cout << -1;
return 0;
}
if (!vis[f[i]])
h[++m] = f[i], g[f[i]] = m,vis[f[i]]=1;
g[i] = g[f[i]];
}
cout << m << endl;
For(i, 1, n) cout << g[i] << " ";
cout << endl;
For(i, 1, m) cout << h[i] << " ";

D. Artsem and Saunders的更多相关文章

  1. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造

    D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...

  2. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders

    地址:http://codeforces.com/contest/765/problem/D 题目: D. Artsem and Saunders time limit per test 2 seco ...

  3. 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]内的 ...

  4. 【构造】Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders

    根据那两个式子 g(h(x))=x h(g(x))=f(x) 可以推出来两个新的式子 g(f(x))=g(x) h(x)=f(h(x)) 于是,我们先找到f(x)的所有不动点,有几个不动点,m就是多少 ...

  5. D. Artsem and Saunders 数学题

    http://codeforces.com/contest/765/problem/D 这题的化简,不能乱带入,因为复合函数的带入,往往要严格根据他们的定义域的 题目要求出下面两个函数 g[h(x)] ...

  6. 【codeforces 765D】Artsem and Saunders

    [题目链接]:http://codeforces.com/contest/765/problem/D [题意] 给你一个函数f(x); 要让你求2个函数g[x]和h[x],使得g[h[x]] = x对 ...

  7. Codeforces_765_D. Artsem and Saunders_(数学)

    D. Artsem and Saunders time limit per test 2 seconds memory limit per test 512 megabytes input stand ...

  8. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined)

    运气好,分到的房里我最先开始Hack C题,Hack了12个,听说F题沙雕莫队但我不会,最后剩不到15分钟想出E题做法打了一波结果挂了,最后虽然上分了但总有点不甘心. 最后A掉ABCD Hack+12 ...

  9. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A B C D 水 模拟 构造

    A. Neverending competitions time limit per test 2 seconds memory limit per test 512 megabytes input ...

随机推荐

  1. 13.Odoo产品分析 (二) – 商业板块(6) –采购(3)

    接上一篇  查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (二) – 商业板块(6) –采购(2) 7. 仓库 仓库是在安装采购管理模块时出现的菜单.用于管理工厂库存,包括已经在手的货物 ...

  2. 解决ie6中png图片格式不兼容问题

    在IE6中对图片格式png24支持度不高,如果使用的图片格式是png24,则会导致透明效果无法正常显示 解决方法: 1.可以使用png8来代替png24,即可解决问题,但是使用png8代替png24以 ...

  3. 四. Redis事务处理

    Redis目前对事务的支持还是比较简单,Redis能保证一个Client发起的事务中的命令可以连续执行,而中间不会插入其他Client的命令:当一个Client在连接中发起一个multi命令的时候,这 ...

  4. Android内存优化(五) Lint代码扫描工具

     1.使用 工具栏 -> Analyze -> Inspect Code… 点击 Inspect Code 后会弹出检查范围的对话框: 默认是检查整个项目,我们可以点击 Custom sc ...

  5. <自动化测试方案_1>第一章、为什么要做自动化测试?(Why)

    第一章.为什么要做自动化测试?(Why) 测试的产品分为:桌面程序(C/S).web应用(B/S) 我们的产品是B/S (一)迭代中省去人力测试非新增功能: 在项目中由于测试时间的限制,测试中只能实现 ...

  6. verilog实现红黄蓝三秒灯

    代码如下 test.v文件 led.v文件 module test(); wire led_r,led_g,led_b; ; clk <= ~clk; led c1 ( .clk(clk), . ...

  7. 章节七、4-Sets

    一.set中不允许存在相同的元素 package ZangJie7; import java.util.ArrayList; import java.util.HashSet; import java ...

  8. peewee基本操作

    本文将简单的介绍Peewee,一览它的主要特点,主要涉及到: 模型定义 存储数据 检索数据 注意:如果你偏爱稍微高级一点的教程, peewee建立类似twitter应用 是一篇结合Flask框架与pe ...

  9. web前端(13)—— 了解JavaScript,JavaScript的引入方式

    从本篇博文开始,将进入web前端方便最关键最重要的部分——javascript,学到后面你就知道它真的太重要了 什么是JavaScript JavaScript一种直译式的脚本语言,是一种动态类型.弱 ...

  10. c/c++ 标准容器 forward_list resize 操作

    c/c++ 标准容器 forward_list, resize, 重新定位迭代器 1,forward_list特有的方法: insert_after emplace_after erase_after ...