Codeforces Round #485 (Div. 2) E. Petr and Permutations

Codeforces Round #485 (Div. 2) E. Petr and Permutations

题目连接:

http://codeforces.com/contest/987/problem/E

Description

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 $1$ to $n$ and then $3n$ 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+1$ times instead of $3n$ 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.

Sample Input

5
2 4 5 1 3

Sample Output

Petr

题意

给定一个排列,问它是从\(1, 2, 3,...,n\)变换\(3n\)次而来,还是\(7n+1\)

Give a random permutation, it maybe transform \(3n\) times or \(7n+1\) times from the permutation of numbers from \(1\) to n. Judging it is \(3n\) or \(7n+1\) ?

题解:

考虑n是奇数的情况,\(3n\)为奇数,\(7n+1\)为偶数;n是偶数时,\(3n\)为偶数,\(7n+1\)为奇数。
考虑最小交换次数,假定为k,\(k+2*t\)次交换都可以变成目标串,但奇偶性相同。所以只需判断最小交换次数的奇偶性即可。

When n is a odd number, \(3n\) is odd,\(7n+1\) is even.When n is a even number, \(3n\) is even,\(7n+1\) is odd.
Consider about the swap times, assume k is the mininum swap times to the final permutation , \(k+2*t\) will reach the final result , \(k\) and \(k+2*t\) have the same parity. So we need to know the parity of the mininum swap times.

如果我们不把stdio的同步关闭,将会读入超时

If we dont close the sync with stdio , time limit exceeded.

代码

#include <bits/stdc++.h>

using namespace std;

int n;
map<int,int> m;
int a[1000010];
int c[1000010];
int ans;
inline int lowbit(int k) {
    return (k&(-k));
}

void update(int t) {
    while (t<n) {
        c[t]++;
        t+=lowbit(t);
    }
}

int sum(int t) {
    int ret=0;
    while (t>0) {
        ret+=c[t];
        t-=lowbit(t);
    }
    return ret;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cerr.tie(nullptr);

    cin>>n;
    for (int i=1;i<=n;i++) {
        cin>>a[i];
        m[a[i]]=i;
    }
    for (int i=n;i;i--) {
        ans += i-(m[i]-sum(m[i]-1));
        update(m[i]);
    }
    if ((n&1)^(ans&1)==0) cout << "Petr" <<endl;
    else cout << "Um_nik" <<endl;
}

猜你喜欢

转载自www.cnblogs.com/EDGsheryl/p/9157964.html