CF986B Petr and Permutations [逆序对]

  题目传送门

Petr and Permutations

格式难调,题面就不放了。

 


  分析:

  胡乱分析+猜测SP性质一波。然后被学长告知:“1~n的排列交换次数与逆序对的奇偶性相同。”然后就愉快地A了。

  因为$3n$和$7n+1$的奇偶性是一定不同的,那么就求逆序对的奇偶性然后判断即可。(太久没打逆序对了,都不会打了。。一开始还打错了。。)

  Code:

 

//It is made by HolseLee on 26th July 2018
//CF986B
#include
using namespace std;

const int N=1e6+7;
int n,a[N],c[N],ans;

inline int lowbit(int x)
{
    return x&-x;
}

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

inline int quary(int x)
{
    int ret=0;
    for(;x>0;x-=lowbit(x))ret+=c[x];
    return ret;
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        insert(a[i],1);
        ans+=i-quary(a[i]);
    }
    ans%=2;
    if(!(n%2))cout<<(ans?"Um_nik":"Petr")<<"\n";
    else cout<<(ans?"Petr":"Um_nik")<<"\n";
    return 0;
}

 

转载于:https://www.cnblogs.com/cytus/p/9369999.html

你可能感兴趣的:(CF986B Petr and Permutations [逆序对])