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 ...
随机推荐
- Android为TV端助力 MediaPlayer API大全已经方法详解(转载)
通过这张图,我们可以知道一个MediaPlayer对象有以下的状态: 1)当一个MediaPlayer对象被刚刚用new操作符创建或是调用了reset()方法后,它就处于Idle状态.当调用了rele ...
- F5负载的应用IIS日志记录的不是真实IP的处理方法
如果没有这一项,在服务里添加上 将F5XForwardedFor.dll拷贝到应用目录下 添加筛选器: 名称:F5XForwardedFor 可执行文件:F5XForwardedFor.dll所在的目 ...
- Android Handler、Message、MessageQueue和Looper官方说明
Handler官方说明 官方API文档:https://developer.android.google.cn/reference/android/os/Handler Handler允许您发送和处理 ...
- nginx的rewrite ,如何在flask项目中获取重写前的url
1. 在flask配一个重写到哪的路由,假设是/rewite/,然后到nginx的配置文件写重写规则,我这里重写全部的请求,接着测试能否重写成功 1. 添加一个路由 配置重写规则 测试成功 2.接下来 ...
- Linux网卡聚合时,其中一个网卡有两种配置的解决方法
先来看看: ficonfig 其中第一网卡是ssh使用: 第二个网卡是在Linux 最小化安装后IP的配置(手动获取静态IP地址)这个文章中配置过ip是192.168.1.2:在Linux重命名网卡名 ...
- hadoop java上传文件
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; impo ...
- sqlserver——cube:多维数据集
1.cube:生成多维数据集,包含各维度可能组合的交叉表格,使用with 关键字连接 with cube 根据需要使用union all 拼接 判断 某一列的null值来自源数据还是 cube 使用G ...
- django数据查询之F查询和Q查询
仅仅靠单一的关键字参数查询已经很难满足查询要求.此时Django为我们提供了F和Q查询: # F 使用查询条件的值,专门取对象中某列值的操作 # from django.db.models impor ...
- Linux进程退出详解(do_exit)--Linux进程的管理与调度(十四)
Linux进程的退出 linux下进程退出的方式 正常退出 从main函数返回return 调用exit 调用_exit 异常退出 调用abort 由信号终止 _exit, exit和_Exit的区别 ...
- emacs 利用 auto-complete 自动补齐
emacs 利用 auto-complete 自动补齐 1,首先导入melpa,在文件~/.emacs中添加下面代码 (require 'package) (package-initialize) ( ...