1. 程式人生 > >Codeforces 987E Petr and Permutations(數組的置換與復原 、結論)

Codeforces 987E Petr and Permutations(數組的置換與復原 、結論)

perm ref con 發現 tar scanf 連接 ++ ()

題目連接: 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(數組的置換與復原 、結論)