zoukankan      html  css  js  c++  java
  • 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 <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 }
    View Code
  • 相关阅读:
    sklearn.model_selection.validation_curve 验证曲线
    sklearn.model_selection.learning_curve学习曲线
    sklearn.pipeline.Pipeline管道简化工作流
    什么时候需要做数据标准化
    小程序获取用户默认地址的代码
    微信小程序--更换用户头像/上传用户头像/更新用户头像
    小程序 image跟view标签上下会有间隙
    小程序宽100%,高自适应怎么做?
    小程序图片点击放大右滑
    小程序can't read property 'push' of undefined
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/9120910.html
Copyright © 2011-2022 走看看