1. 程式人生 > >Codeforces Round #485 (Div. 1) B. Petr and Permutations

Codeforces Round #485 (Div. 1) B. Petr and Permutations

序列 ios cti 次數 order namespace img memory trie

B. Petr and Permutations time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output

Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from

11 to nn and then 3n3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+17n+1 times instead of
3n3n times. Because it is more random, OK?!

You somehow get a test from one of these problems and now you want to know from which one.

Input

In the first line of input there is one integer nn (103n106103≤n≤106).

In the second line there are nn distinct integers between 11 and nn — the permutation of size

nn from the test.

It is guaranteed that all tests except for sample are generated this way: First we choose nn — the size of the permutation. Then we randomly choose a method to generate a permutation — the one of Petr or the one of Alex. Then we generate a permutation using chosen method.

Output

If the test is generated via Petr‘s method print "Petr" (without quotes). If the test is generated via Alex‘s method print "Um_nik" (without quotes).

Example input Copy
5
2 4 5 1 3
output Copy
Petr
Note

Please note that the sample is not a valid test (because of limitations for nn) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.

Due to randomness of input hacks in this problem are forbidden.

思路:逆序對的奇偶性等價於交換次數。顯然可以樹狀數組求逆序對個數。看完題解學到一個O(n)的做法,對於序列a,從i向a[i]連條邊,在這個n條邊的圖中統計環的個數。環個數變化的奇偶性就等於逆序對變化的奇偶性。

因為對於每次交換,如果兩個點在一個環中,那麽環就會被拆分成兩個;如果兩個點在兩個環中,操作後就會變成一個環。代碼很簡單,直接粘的題解給的代碼。

技術分享圖片
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 #include <set>
 8 #include <map>
 9 #include <unordered_set>
10 #include <unordered_map>
11 #include <queue>
12 #include <ctime>
13 #include <cassert>
14 #include <complex>
15 #include <string>
16 #include <cstring>
17 using namespace std;
18 
19 #ifdef LOCAL
20     #define eprintf(...) fprintf(stderr, __VA_ARGS__)
21 #else
22     #define eprintf(...) 42
23 #endif
24 
25 typedef long long ll;
26 typedef pair<int, int> pii;
27 #define mp make_pair
28 
29 const int N = (int)1e6 + 7;
30 int n;
31 int a[N];
32 int ans;
33 
34 int main()
35 {
36 //    freopen("input.txt", "r", stdin);
37 //    freopen("output.txt", "w", stdout);
38 
39     scanf("%d", &n);
40     for (int i = 0; i < n; i++) {
41         scanf("%d", &a[i]);
42         a[i]--;
43     }
44     ans = 0;
45     for (int i = 0; i < n; i++) {
46         if (a[i] == -1) continue;
47         ans ^= 1;
48         int x = i;
49         while(x != -1) {
50             int y = a[x];
51             a[x] = -1;
52             x = y;
53         }
54     }
55     if (ans)
56         printf("Um_nik\n");
57     else
58         printf("Petr\n");
59 
60     return 0;
61 }
View Code

Codeforces Round #485 (Div. 1) B. Petr and Permutations