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

链接: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 ";
            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;
}
 
原文地址:https://www.cnblogs.com/sweetlittlebaby/p/12722693.html