[CF986B]Petr and Permutations

题目大意:有一个$1\sim n$的序列,若可以用$3n$次交换产生,则输出$\text{Petr}$,若可以用$7n$次交换则输出$\text{Um_nik}$。

题解:交换一次会导致逆序对的奇偶性变化,于是若逆序对的奇偶性和$3n$(即$n$)相同输出$\text{Petr}$,否则输出$\text{Um_nik}$

卡点:求成了顺序对

 

C++ Code:

#include 
#define maxn 1000010
int n, ans;
int s[maxn];
namespace BIT {
	int tr[maxn], res;
	inline void add(int p, int a = 1) {for (; p <= n; p += p & (-p)) tr[p] += a;}
	inline int ask(int p) {for (res = 0; p; p &= p - 1) res += tr[p]; return res;}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", s + i);
	for (int i = n; i; i--) {
		ans += BIT::ask(s[i]);
		BIT::add(s[i]);
	}
	if ((ans & 1) == (n & 1)) puts("Petr");
	else puts("Um_nik");
	return 0;
}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/9745578.html

你可能感兴趣的:(c/c++)