D. Artsem and Saunders
一个变换题
给定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的更多相关文章
- 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 ...
- 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 ...
- 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]内的 ...
- 【构造】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就是多少 ...
- D. Artsem and Saunders 数学题
http://codeforces.com/contest/765/problem/D 这题的化简,不能乱带入,因为复合函数的带入,往往要严格根据他们的定义域的 题目要求出下面两个函数 g[h(x)] ...
- 【codeforces 765D】Artsem and Saunders
[题目链接]:http://codeforces.com/contest/765/problem/D [题意] 给你一个函数f(x); 要让你求2个函数g[x]和h[x],使得g[h[x]] = x对 ...
- Codeforces_765_D. Artsem and Saunders_(数学)
D. Artsem and Saunders time limit per test 2 seconds memory limit per test 512 megabytes input stand ...
- 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 ...
- 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 ...
随机推荐
- IDEA项目搭建十三——服务消费端与生产端通信实现
一.简介 之前已经完成了EurekaClient的服务生产者和Feign的服务消费者模块的搭建,现在实现统一的通信约定 (1) 统一Request结构 (2) 统一Response结构 (3) 统一E ...
- Android为TV端助力 帧动画
首先在res/drawable/name1.xml/定义一组图片集合: <?xml version="1.0" encoding="utf-8"?> ...
- <1>Linux日志查找方法
Linux日志查找方法 适用于测试,开发,运维人员,用来查找Linux服务器问题的一般方法,比较实用,如果有更好的办法可以一块讨论,欢迎大神们来指导哈!!! 进入正题 第一步.通过Xshell登录服务 ...
- RecyclerView 高度不能随着Item数量 自适应高度
在最近项目中遇到 ,在RecyclerView加载list数据时,高度无法自适应增长,看了很多博客,各种尝试,都没有解决这个问题,在某个博客中,讲到此解决方法,在此记录下. 即在RecyclerVie ...
- git 入门教程之紧急修复
和往常一样,每个人团队开发者都在自己的本地分支上进行日常工作,相互独立又相互联系,一直以来相安无事,可是某天下午,上级领导突然急冲冲的打电话告诉你线上出bug了,需要你紧急修复,下班之前必须解决! 我 ...
- (网页)HTMl5的sessionStorage和localStorage
百度上百度的,记录一下: html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage. sessionStorage用于本地存储一个会话(sessi ...
- Spark DataFrame的groupBy vs groupByKey
在使用Spark SQL的过程中,经常会用到groupBy这个函数进行一些统计工作.但是会发现除了groupBy外,还有一个groupByKey(注意RDD也有一个groupByKey,而这里的gro ...
- 【redis专题(9)】事务
Redis支持简单的事务,所谓简单是因为其不支持回滚(回滚是用队列模仿的),与mysql有以下区别 rollback与discard的区别: 如果已经成功执行了2条语句, 第3条语句出错 Rollba ...
- MySQL 5.7安装指南
1.下载 1)进入官网下载5.7.23压缩包 下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 2.安装与配置 1)将下载的压 ...
- selenium RC 环境配置
摘自http://www.cnblogs.com/sanzangTst/p/7452922.html 收藏学习 学习selenium python需要的工具: 1.浏览器 2.Python 3.Sel ...