码迷,mamicode.com
首页 > 编程语言 > 详细

Codeforces 987E Petr and Permutations(数组的置换与复原 、结论)

时间:2018-08-10 17:10:10      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:bsp   ref   另一个   long   return   while   如何   air   get   

题目连接: Petr and Permutations

题意:给出一个1到n的序列,Petr打乱了3n次,Um_nik打乱了7n+1次,现在给出被打乱后的序列,求是谁打乱的。

题解:因为给出了一个3*n和一个7*n+1,发现这两个当一个为奇数另一个一定为偶数,所以可以联想和奇偶性质有关。但是这里面要算最短几步能把当前的序列变成1-n。这里我算错~~顺便学了一下如何将置换序列复原。

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long LL;
const int MAX_N = 1e6+9;
const int INF =1e9+7;
int N,M,T,S;
int vec[MAX_N];
int main(){
    while(cin>>N)
    {
        for(int i=1;i<=N;i++) scanf("%d",&vec[i]);
        int num = 0;
        for (int i = 1; i <= N; i++)
        {
            while (vec[i] != i)
            {
                swap(vec[i], vec[vec[i]]);
                num++;
            }
        }
        if((3*N - num) % 2 == 0) cout<<"Petr"<<endl;
        else cout<<"Um_nik"<<endl;
    }
    return 0;
}

 

Codeforces 987E Petr and Permutations(数组的置换与复原 、结论)

标签:bsp   ref   另一个   long   return   while   如何   air   get   

原文地址:https://www.cnblogs.com/doggod/p/9455770.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!