Codeforces Round #485 (Div. 1) B. Petr and Permutations_ba82586628365094的博客-程序员秘密

技术标签: 数据结构与算法  

B. Petr and Permutations
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

In the first line of input there is one integer nn (103n106103≤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.

Output

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).

Example
input
Copy
5
2 4 5 1 3
output
Copy
Petr
Note

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.

思路:逆序对的奇偶性等价于交换次数。显然可以树状数组求逆序对个数。看完题解学到一个O(n)的做法,对于序列a,从i向a[i]连条边,在这个n条边的图中统计环的个数。环个数变化的奇偶性就等于逆序对变化的奇偶性。

因为对于每次交换,如果两个点在一个环中,那么环就会被拆分成两个;如果两个点在两个环中,操作后就会变成一个环。代码很简单,直接粘的题解给的代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <vector>
 7 #include <set>
 8 #include <map>
 9 #include <unordered_set>
10 #include <unordered_map>
11 #include <queue>
12 #include <ctime>
13 #include <cassert>
14 #include <complex>
15 #include <string>
16 #include <cstring>
17 using namespace std;
18 
19 #ifdef LOCAL
20     #define eprintf(...) fprintf(stderr, __VA_ARGS__)
21 #else
22     #define eprintf(...) 42
23 #endif
24 
25 typedef long long ll;
26 typedef pair<int, int> pii;
27 #define mp make_pair
28 
29 const int N = (int)1e6 + 7;
30 int n;
31 int a[N];
32 int ans;
33 
34 int main()
35 {
36 //    freopen("input.txt", "r", stdin);
37 //    freopen("output.txt", "w", stdout);
38 
39     scanf("%d", &n);
40     for (int i = 0; i < n; i++) {
41         scanf("%d", &a[i]);
42         a[i]--;
43     }
44     ans = 0;
45     for (int i = 0; i < n; i++) {
46         if (a[i] == -1) continue;
47         ans ^= 1;
48         int x = i;
49         while(x != -1) {
50             int y = a[x];
51             a[x] = -1;
52             x = y;
53         }
54     }
55     if (ans)
56         printf("Um_nik\n");
57     else
58         printf("Petr\n");
59 
60     return 0;
61 }
View Code

 

转载于:https://www.cnblogs.com/BIGTOM/p/9120910.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ba82586628365094/article/details/101622120

智能推荐

ROS+OpenCV 人脸识别,物体识别_ros人脸识别_JJH的创世纪的博客-程序员秘密

程序如下,使用方式同人脸识别,在scripts目录放入程序,在launch目录放入launch文件。首先准备ROS系统,基于ros的软件支持opencv,usbcam。被识别出的物体有篮球,柜子,手臂,衣袖,被识别物体用边框圈出。face_detector.launch运行人脸识别程序。$rqt_image_view //也可用rviz订阅。scripts存放代码,launch存放启动文件。usb_cam.launch开启摄像头。分别开启三个终端,执行以下命令。在创建功能包时导入依赖库。

Golang学习(二)time包相关_go docopt.parse用法_Codex_97的博客-程序员秘密

1.获取当前时间的时间戳time.Now().Unix()2.日期时间格式化str格式化时间`fmt.Println (time.Now().Format("2006-01-02 15:04:05"))时间戳转str格式化时间t:= time.Now()fmt.Print(time.Unix(t.Unix() , 0).Format("2006/01/02 (15:0...

HDU 1166 敌兵布阵 线段树单点更新求和_weixin_30267697的博客-程序员秘密

题目链接中文题,线段树入门题,单点更新求和,建一棵树就可以了。#include &lt;iostream&gt;#include &lt;cstdio&gt;#include &lt;cmath&gt;#include &lt;cstring&gt;#include &lt;algorithm&gt;#define N 50005using namespace ...

LeetCode 46. 全排列_亓官劼的博客-程序员秘密

LeetCode 46. 全排列  大家好,我叫亓官劼(qí guān jié ),在CSDN中记录学习的点滴历程,时光荏苒,未来可期,加油~博客地址为:亓官劼的博客本文原创为亓官劼,请大家支持原创,部分平台一直在盗取博主的文章!!!博主目前仅在CSDN中写博客,唯一博客更新的地址为:亓官劼的博客题目难度中等给定一个 没有重复 数字的序列,返回其所有可能的全排列。示例:输...

LaText中插入带上下限的求和符号_weixin_30851867的博客-程序员秘密

效果如下:LaTex命令如下:\begin{equation} \label{8} z_{i}(k+1)=\sum_{j\in N_{i}(k)} a_{ij}(k)z_{i}(k),z_{i}(0)=z_{i0}\end{equation}转载于:https://www.cnblogs.com/TTTTT/p/6413537.html...

oracle ORA-01000: maximum open cursors exceeded问题的解决方法_iadink的博客-程序员秘密

项目在运行过程中,后台报错:                     Java代码  ORA-01000: maximum open cursors exceeded  ORA-00604: error occurred at recursive SQL level 1  ORA-01000: maximum open cursors exceeded  O

随便推点

逆向基础 OS-specific (一)_weixin_33912246的博客-程序员秘密

左懒 · 2015/08/13 13:0064章 传递参数的方法64.1 cdcel这种传递参数的方法在C/C++语言里面比较流行。如下的代码片段所示,调用者反序地把参数压到栈中:最后一个参数,倒数第二个参数,第一个参数。调用者还必须在函数返回之后把栈指针(ESP)还原为初始状态。Listing 64.1: cdeclpush arg3push arg2push arg1call funct...

简体中文的MSI形式的安装程序显示乱码的处理_weixin_34087301的博客-程序员秘密

AppLocale在简体中文系统里使用之后,会令某些简体中文的MSI形式的安装程序显示乱码(比如:OFFICE2000简体中文版安装程序).解决方法:方法一:卸载AppLocale即可解决;方法二:删除AppLocale安装目录下的一个临时文件:\WINDOWS\AppPatch\AppLoc.tmp(此文件只有4字节)即可解决,无需卸载AppLocale;方法三:App...

java ssl连接失败_java - 如何使Java 6与“SSL对等关闭不正确”的SSL连接失败,成功与Java 7一样? - 堆栈内存溢出..._吴思扬的博客-程序员秘密

我看到运行Java 6的客户端的SSL连接失败,例外情况如下:Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshakeat com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java...

RSA加密失败 java.security.InvalidKeyException: IOException: Detect premature EOF_EnumaElish666的博客-程序员秘密

/** * RSA公钥加密 * @param str * 加密字符串 * @param publicKey * 公钥 * @return 密文 * @throws Exception * 加密过程中的异常信息 */public static String encrypt( String str, String...

前端 websocket长连接请求数据_前端长连接_管家金闪闪的博客-程序员秘密

前端 websocket长连接,前端实现方法,需后台配合定义在methods的方法getSoket(){ let that = this; let socket; if(typeof(WebSocket) == "undefined") { console.log("您的浏览器不支持WebSocket"); }else{ console.log("您的浏览器支持WebSocket"); //浏览器支持的、请求

线性回归,损失的定义,损失函数与优化方法,用统计学习方法来理解线性回归、损失函数和优化方法,Sklearn使用方法_sklearn线性回归损失函数_JJH的创世纪的博客-程序员秘密

目录1.线性回归一元线性关系多元线性关系2.损失:评估预测结果与真实值的偏差程度误差累积的结果总损失计算公式:损失函数:最小二乘法3.损失函数的优化方法4.用统计学习方法来理解线性回归、损失函数和优化方法5.Sklearn API接口与使用方式波士顿房价预测案例参考文档1.线性回归线性回归属于监督学习的回归问题的一类,由于回归问题的目标值是连续的,所以线性回归算法的目标是寻找连续的目标值之间的一种趋势,通过获取这种趋势来建立模型。得到测试集后,将特征值

推荐文章

热门文章

相关标签