Petr and Permutations CodeForces - 987E(逆序对)

题意:

  给出一个长度为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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值