首页 > Web开发 > 详细

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

时间:2020-04-17 23:14:55      阅读:70      评论:0      收藏:0      [点我收藏+]

链接:https://codeforces.com/contest/765/problem/D

题意;给n个数,分别是f(i),让你找n个g(i)和m(任意)个h(i),

其中,g(h(x))=x,h(g(x))=f(x);

题解:推一下这个式子,可得到h(x)=f(h(x)),g(x)=g(f(x));

从而推出,f(x)=f(f(x));判定h函数是否存在,h函数即为f函数的去重;

code:


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int a[maxn],b[maxn],c[maxn];
int n,m;
map<int,int> mp;
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++) cin>>a[i];
    for(int i=1; i<=n; i++)
    {
        if(a[a[i]]!=a[i])
        {
            cout<<"-1\n";
            return 0;
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(mp[a[i]]==0)
        {
            m++;//m为所对应的那个x
            mp[a[i]]=m;
            c[m]=a[i];
        }
        b[i]=mp[a[i]];//如果a[i]出现过,这一步直接让它等于之前的那个即可;
    }
    cout<<m<<endl;
    for(int i=1; i<=n; i++)
     cout<<b[i]<<" ";
    cout<<endl;
    for(int i=1; i<=m; i++)
     cout<<c[i]<<" ";
     cout<<endl;
    //system("pause");
    return 0;
}
 

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

原文:https://www.cnblogs.com/sweetlittlebaby/p/12722693.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!