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

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 
 2 #include 
 3 #include 
 4 #include 
 5 #include 
 6 #include 
 7 #include <set>
 8 #include 
 9 #include 
10 #include 
11 #include 
12 #include 
13 #include 
14 #include 
15 #include <string>
16 #include 
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

 

转载于:https://www.cnblogs.com/BIGTOM/p/9120910.html

你可能感兴趣的:(Codeforces Round #485 (Div. 1) B. Petr and Permutations)