题意翻译

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

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

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

感谢@AKEE 提供翻译

题目描述

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 1 1 1 to n n n and then 3n 3n 3n 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+1 7n+1 7n+1 times instead of 3n 3n 3n 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 n n n ( 103≤n≤106 10^{3} \le n \le 10^{6} 103≤n≤106 ).

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

It is guaranteed that all tests except for sample are generated this way: First we choose n n n
— 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).

输入输出样例

输入样例#1:

5
2 4 5 1 3
输出样例#1:

Petr

说明

Please note that the sample is not a valid test (because of limitations for n n n ) 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.

Solution:

  本题思路贼有意思。

  开始题意没理解,所以简述下题意:给你一个$1——n$的排列,它是由初始为从$1——n$的序列每次交换任意两位得到的,然后A进行了$3n$次操作,$B$进行了$7n+1$次操作,要你判断这个排列是谁操作出来的。

  不难发现$3n$和$7n+1$奇偶性一定互异(显然),然后有一个很重要的性质,那就是排列中逆序数的奇偶性和操作的奇偶性相同,证明:1、首先每次若交换相邻的两位,则必然使得逆序数$+1$或$-1$; 2、任意交换两位,可以理解为先将后面的一个向前移动$k$位,再将前面要移的一位向后移$k-1$位,注意到共移动了$2k-1$次,所以移动一次逆序对变化数量为奇数,那么奇数次移动逆序对数就是奇数,偶数次移动逆序对数就是偶数了。

  于是我们直接树状数组求下逆序数,然后判断一下就好了。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=1e6+;
int t[N+],a[N],n;
bool f; il int gi(){
int a=;char x=getchar();
while(x<''||x>'')x=getchar();
while(x>=''&&x<='')a=(a<<)+(a<<)+x-,x=getchar();
return a;
} il void update(int k){while(k<=n)t[k]++,k+=k&-k;} il int query(int k){int sum=;while(k)sum+=t[k],k-=k&-k;return sum;} int main(){
n=gi();
For(i,,n) a[i]=gi();
Bor(i,,n) {
f^=(query(a[i]-)&);
update(a[i]);
}
if(n&) f?puts("Petr"):puts("Um_nik");
else f?puts("Um_nik"):puts("Petr");
return ;
}

CF986B Petr and Permutations的更多相关文章

  1. CF986B Petr and Permutations [逆序对]

    题目传送门 Petr and Permutations 格式难调,题面就不放了. 分析: 胡乱分析+猜测SP性质一波.然后被学长告知:“1~n的排列交换次数与逆序对的奇偶性相同.”然后就愉快地A了. ...

  2. CF986B Petr and Permutations 思维

    每次交换:逆序对的数量+1或者-1: 假设最后逆序对数量为 sum; ①x+y=3n; ②x-y=sum; -> 3n+sum为偶数: 所以 n 和 sum 必须奇偶一样: #include&l ...

  3. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  4. Codeforces 987E Petr and Permutations(数组的置换与复原 、结论)

    题目连接: Petr and Permutations 题意:给出一个1到n的序列,Petr打乱了3n次,Um_nik打乱了7n+1次,现在给出被打乱后的序列,求是谁打乱的. 题解:因为给出了一个3* ...

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

    题意: 给出一个长度为n的序列,求出是谁操作的(原序列为从小到大的序列),Peter的操作次数为3n,Alex的操作次数为7n+1 解析: 我们来看这个序列中的逆序对,逆序对的个数为偶数则操作次数为偶 ...

  6. CodeForces - 987E Petr and Permutations (思维+逆序对)

    题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归 ...

  7. 【Codeforces 986B】Petr and Permutations

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] n为奇数时3n和7n+1奇偶性不同 n为偶数时也是如此 然后交换任意一对数 逆序对的对数的奇偶性会发生改变一次 求出逆序对 对n讨论得出答案. ...

  8. Codeforces 986B. Petr and Permutations(没想到这道2250分的题这么简单,早知道就先做了)

    这题真的只能靠直觉了,我没法给出详细证明. 解题思路: 1.交换3n次或者7n+1次,一定会出现一个为奇数,另一个为偶数. 2.用最朴素的方法,将n个数字归位,计算交换次数. 3.判断交换次数是否与3 ...

  9. Codeforces 986B - Petr and Permutations

    Description\text{Description}Description Given an array a[], swap random 2 number of them for 3n or  ...

随机推荐

  1. POJ2505 A multiplication game(博弈)

    题意 开始时$p = 1$,每次可以乘$2 - 9$,第一个使得$p \geqslant n$的人赢 问先手是否必胜 $1 <n <4294967295$ Sol 认真的推理一波. 若当前 ...

  2. 【NIS】深入了解NIS

    1  简介 NIS( NetworkInformation Service)提供了一个网络黄页的功能,当用户登录系统时,Linux系统会到NIS主机上去寻找用户使用的帐号密码信息加以比对,以提供用户登 ...

  3. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  4. 一种精准monkey测试的方法

    WeTest 导读 相信大家都知道移动端应用的monkey测试吧,不知你们有没有为monkey测试的太过于随机性的特性有过困扰,至少在我们这种界面控件较少且控件位置较偏的app的使用上其测试有效性大打 ...

  5. 初识c++模板元编程

    模板元编程(Template metaprogramming,简称TMP)是编译器内执行的程序,编译器读入template,编译输出的结果再与其他源码一起经过普通编译过程生成目标文件.通俗来说,普通运 ...

  6. Objective-C 内存管理和ARC

    内存管理 范围: 任何继承了NSObject的对象 对基本数据类型无效 原理: 每个对象内部都保存了一个与之相关联的整数 称为引用计数器 1.计数器的基本操作 当使用alloc new或者copy创建 ...

  7. TW实习日记:第20-21天

    为什么上周五没写呢,因为上周五一直在熟悉业务流程...根本不会写一些复杂的业务代码,因为没有业务流程图!!!在学校的上需求分析和UML建模课的时候,还有软件工程课的时候,想着这都什么鬼啊,听来干嘛,写 ...

  8. 硬盘基础知识&&分区

    学习记录的笔记,虽然毫无章法 硬盘基础知识 磁盘的物理组成 如下图所示: 有关磁盘物理知识的详细介绍请看硬盘的存储原理和内部架构这篇博文 硬盘接口类型 IDE SATA SCSI SAS 光纤通道 I ...

  9. centos 6.5 启动时卡在进度条位置无法进入系统解决办法。

    今天公司服务器因突然断电导致phddns 花生壳 启动失败,一直卡在启动进度条页面. 解决办法 1.按F5查看卡在什么位置, 2.查看解决方法:程序卡住的情况下,直接备份资料后,卸载程序重启就可以了. ...

  10. opencv-学习笔记(5)形态学转变

    opencv-学习笔记(4)形态学转变 本章讲了几种形态学操作 腐蚀erode 膨胀dilate 开运算MORPH_OPEN 闭运算MORPH_CLOSE 形态学梯度MORPH_GRADIENT 礼帽 ...