1. 程式人生 > >Codeforces 986B. Petr and Permutations(沒想到這道2250分的題這麼簡單,早知道就先做了)

Codeforces 986B. Petr and Permutations(沒想到這道2250分的題這麼簡單,早知道就先做了)

這題真的只能靠直覺了,我沒法給出詳細證明。

解題思路:

  1.交換3n次或者7n+1次,一定會出現一個為奇數,另一個為偶數。

  2.用最樸素的方法,將n個數字歸位,計算交換次數。

  3.判斷交換次數是否與3n的奇偶性相同,相同輸出Petr;

    不相同則一定與7n+1的奇偶性相同,輸出Um_nik。

程式碼:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int a[1000010];
int idx[1000010];
int main(){
    ios::sync_with_stdio(false
); int n; cin >> n; for(int i = 1;i <= n; ++i) cin >> a[i],idx[a[i]] = i; int cot = 0; for(int i = 1;i <= n; ++i){ if(a[i] != i){ a[idx[i]] = a[i]; idx[a[i]] = idx[i]; cot++; } } if((cot+3*n)&1){ cout
<< "Um_nik" << endl; }else{ cout << "Petr" << endl; } return 0; }