Codeforces Round #485 (Div. 1) B. Petr and Permutations
Posted hit_yjl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #485 (Div. 1) B. Petr and Permutations相关的知识,希望对你有一定的参考价值。
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.
In the first line of input there is one integer nn (103≤n≤106103≤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.
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).
5
2 4 5 1 3
Petr
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 "); 57 else 58 printf("Petr "); 59 60 return 0; 61 }
以上是关于Codeforces Round #485 (Div. 1) B. Petr and Permutations的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #485 (Div. 2) C. Three displays
Codeforces Round #485 (Div. 2) E. Petr and Permutations
Codeforces Round #485 (Div. 2) C Three displays