Petr and Permutations CodeForces

题意:

  给出一个长度为n的序列,求出是谁操作的(原序列为从小到大的序列),Peter的操作次数为3n,Alex的操作次数为7n+1

解析:

  我们来看这个序列中的逆序对,逆序对的个数为偶数则操作次数为偶数,逆序对的个数为奇数,则操作次数为奇数

然后树状数组求逆序对即可

#include <bits/stdc++.h>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1e6+10, INF = 0x7fffffff;
int c[maxn];
int n;
int lowbit(int x)
{
    return x & (-x);
}

void add(int x, int y)
{
    for(int i=x; i<=n; i+=lowbit(i))
        c[i] += y;
}

int get_sum(int x)
{
    int res = 0;
    for(int i=x; i>0; i-=lowbit(i))
        res += c[i];
    return res;
}

int main()
{
    int x;
    int res = 0;
    cin>> n;
    for(int i=0; i<n; i++)
    {
        cin>> x;
        int ans = get_sum(x);
        res += i - ans;
        add(x, 1);
    }
    int a = 3 * n, b = 7 * n + 1;
    if(res & 1)
    {
        if(a & 1)
            cout<< "Petr" <<endl;
        else
            cout<< "Um_nik" <<endl;
    }
    else
    {
        if(a & 1)
            cout<< "Um_nik" <<endl;
        else
            cout<< "Petr" <<endl;
    }

    return 0;
}
自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
原文地址:https://www.cnblogs.com/WTSRUVF/p/9524621.html