欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CodeForces - 987E Petr and Permutations(树状数组+逆序对定理)

程序员文章站 2022-03-08 15:49:47
题目链接:点击查看题目大意:给出一个长度为 n 的序列,可能打乱过 3 * n 次,也可能打乱过 7 * n + 1 次,问到底打乱过多少次题目分析:首先看出,3 * n 和 7 * n + 1 的奇偶是不同的,如此根据逆序对定理,可以根据逆序对的奇偶来判断对换次数,直接用树状数组求出逆序对的个数就好了代码://#pragma GCC optimize(2)//#pragma GCC optimize("Ofast","inline","-ffast-math")//#pragma....

题目链接:点击查看

题目大意:给出一个长度为 n 的序列,可能打乱过 3 * n 次,也可能打乱过 7 * n + 1 次,问到底打乱过多少次

题目分析:首先看出,3 * n 和 7 * n + 1 的奇偶是不同的,如此根据逆序对定理,可以根据逆序对的奇偶来判断对换次数,直接用树状数组求出逆序对的个数就好了

代码:
 

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
using namespace std;

typedef long long LL;

typedef unsigned long long ull;

const int inf=0x3f3f3f3f;

const int N=1e6+100;

int n,c[N];

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

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

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

int main()
{
#ifndef ONLINE_JUDGE
//  freopen("data.ans.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);
	scanf("%d",&n);
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);
		ans=(ans+ask(n)-ask(x))%2;
		add(x);
	}
	if(ans^(3*n%2))
		puts("Um_nik");
	else
		puts("Petr");















    return 0;
}

 

本文地址:https://blog.csdn.net/qq_45458915/article/details/110678282

相关标签: 树状数组