B. Petr and Permutations(思维+逆序对+奇偶性)

https://codeforces.com/problemset/problem/986/B

题目描述

Petr likes to come up with problems about randomly generated data. This time problem is about random permutation. He decided to generate a random permutation this way: he takes identity permutation of numbers from 11 to nn and then 3n3n times takes a random pair of different elements and swaps them. Alex envies Petr and tries to imitate him in all kind of things. Alex has also come up with a problem about random permutation. He generates a random permutation just like Petr but swaps elements 7n+17n+1 times instead of 3n3n times. Because it is more random, OK?!

You somehow get a test from one of these problems and now you want to know from which one.

输入格式

In the first line of input there is one integer nn ( 10^{3} \le n \le 10^{6}103≤n≤106 ).

In the second line there are nn distinct integers between 11 and nn — the permutation of size nn from the test.

It is guaranteed that all tests except for sample are generated this way: First we choose nn — the size of the permutation. Then we randomly choose a method to generate a permutation — the one of Petr or the one of Alex. Then we generate a permutation using chosen method.

输出格式

If the test is generated via Petr's method print "Petr" (without quotes). If the test is generated via Alex's method print "Um_nik" (without quotes).

题意翻译

Petr要打乱排列。他首先有一个从 11 到 nn 的顺序排列,然后进行 3n3n 次操作,每次选两个数并交换它们。

Alex也要打乱排列。他与Petr唯一的不同是他进行 7n+17n+1 次操作。

给定一个 11 到 nn 的排列。问是由谁打乱的。如果是Petr,输出"Petr",否则输出"Um_nik"(不是Alex)

感谢@AKEE 提供翻译

输入输出样例

输入 #1复制

5
2 4 5 1 3

输出 #1复制

Petr

说明/提示

Please note that the sample is not a valid test (because of limitations for nn ) and is given only to illustrate input/output format. Your program still has to print correct answer to this test to get AC.

Due to randomness of input hacks in this problem are forbidden.


开始看到题目一脸懵逼.毫无思路。但是看到3*n和7*n+1这么特殊的两个数感觉会考奇偶性..

思路:交换两个数会导致逆序对个数+1/-1.设+1的个数为x,-1的个数为y.

设k为逆序对数量

那么x+y=3*n.x-y=k(这里y-x=k得出的结论是一样的)

2*x=3*n+k.也就是3*n+k是偶数,那么3*n和k的奇偶性是一致的。

求一下i<j&&a[i]>a[j]的k逆序对个数再和3*n判断一下奇偶性一样不一样就好了。

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define lowbit(x) x&(-x);
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e6+100;
typedef long long LL;
LL tree[maxn],a[maxn],n;
void add(LL x,LL d)
{
	while(x<=n)
	{
		tree[x]+=d;
		x+=lowbit(x);
	}
}
LL getsum(LL x)
{
	LL sum=0;
	while(x>0)
	{
		sum+=tree[x];
		x-=lowbit(x);
	}
	return sum;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>n;
  for(LL i=1;i<=n;i++) cin>>a[i];
  LL ans=0;
  for(LL i=1;i<=n;i++){
  	add(a[i],1);
  	ans+=i-getsum(a[i]);
  }
 // debug(ans);
  if( ( (3*n)%2 )==(ans%2) ){
  	cout<<"Petr"<<endl;
  }
  else{
  	cout<<"Um_nik"<<endl;
  }
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108523238