Codeforces 765D Artsem and Saunders 构造
来源:互联网 发布:伊芙蕾雅淘宝店 编辑:程序博客网 时间:2024/06/11 03:19
点击打开链接
题意:给出n 和 f[] 求出函数g,h 满足 g[h[x]]=x && h[g[x]]=f[x] h定义域为1~m 值域为1~n
g[h[g[x]]]=g[f[x]]=g[x]
g[x]=g[f[x]] -> h[g[x]]=f[x]=h[g[f[x]]]=f[f[x]] 得当两个等式成立即有解时:有f[x]=f[f[x]]
构造
f[x]=f[f[x]] 若f[x]=y 则f[x]=f[y]=y时成立
假设:若h[m]=x 则g[h[m]]=g[x]=m -> h[g[x]]=f[x]=h[m]=x 则f[x]=x
当x=f[x]时:令h[m]=x g[x]=m 则条件g[h[m]]=g[x]=m , h[g[x]]=h[m]=x=f[x]也成立
若g[y]还未求出则 由于f[y]=f[x]=x g[f[y]]=g[f[x]] 由于g[f[x]]在x==f[x]时求出 由前面等式知道令g[y]=g[f[y]]即可
#include <bits/stdc++.h>using namespace std;typedef long long ll;const int N=2e6+50;int f[N],g[N],h[N];int main(){int n;while(cin>>n){bool flag=true;int m=0;memset(g,0,sizeof(g));for(int i=1;i<=n;i++)scanf("%d",&f[i]);for(int i=1;i<=n;i++){if(f[i]!=f[f[i]])flag=false;if(f[i]==i){h[++m]=i;g[i]=m;}}for(int i=1;i<=n;i++){if(g[i]==0)g[i]=g[f[i]]; }if(flag){cout<<m<<endl;for(int i=1;i<=n;i++)printf("%d ",g[i]);printf("\n");for(int i=1;i<=m;i++)printf("%d ",h[i]);printf("\n");}elseputs("-1");}return 0;}
0 0
- Codeforces 765D Artsem and Saunders 构造
- Codeforces 765D Artsem and Saunders 【构造】
- codeforces 765 D Artsem and Saunders(构造)
- 【CodeForces 765D】 Artsem and Saunders(数学,构造)
- 【codeforces 765D】Artsem and Saunders
- codeforces 765D. Artsem and Saunders
- codeforces #397 D. Artsem and Saunders(构造)
- 【Codeforces 756 D. Artsem and Saunders】+ 思维 + 构造
- Codeforces Round #397 D. Artsem and Saunders(构造)
- Codeforces 765D Artsem and Saunders(数学)
- codeforces 397div2 D Artsem and Saunders
- codeforces 397div2 D Artsem and Saunders
- codeforces 397div2 D Artsem and Saunders
- codeforces 397div2 D Artsem and Saunders
- D. Artsem and Saunders
- D. Artsem and Saunders
- D. Artsem and Saunders
- D. Artsem and Saunders
- Spring入门——Resource
- Hibernate 笔记大全二
- a href=#与 a href=javascript:void(0) 的区别
- logstash grok
- Android 4.0新的广播机制FLAG_EXCLUDE_STOPPED_PACKAGES
- Codeforces 765D Artsem and Saunders 构造
- LeetCode 8. String to Integer (atoi)
- retrofit 学习和使用
- CI3框架初识-钩子
- nginx 日志配置与分割
- AndroidManifest.xml 清单文件
- Linux NTP config
- iOS设计模式-建造者模式
- android控件拖动,移动、解决父布局重绘时控件回到原点