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

智能推荐

通过putty和xshell登录虚拟机linux_用putty或xshell远程到虚拟机_codepencil的博客-程序员秘密

使用密码和密钥远程连接Linuxputty和xshell下载完成解压后,打开putty.exe,第一次使用弹出PuTTY Configuration对话框,进行如下配置:选

Typescript中的问号点(?.)是什么意思?_typescript ?._main方 法的博客-程序员秘密

问题Typescript中的感叹号点、问号点是什么意思?我刚入坑react项目的时候看别人代码就看到这样的写法,以开始是懵逼的哈哈哈哈,毕竟是个小白,然后吭哧吭哧的百度查资料,最后发现了问号点(?.)奥义哈哈哈。例子data入参可能为null,undefined,通常我们的写法是直接上if判断啥的,然后再取data中的属性,但是有了问号点(?.)写法就简单很多了,看下面例子:1.typescript写法://1.data可能为null,undefined , row也可能为null,undefin

linux 安装oracle 11g R2 11.2.0.3_JamesFen的博客-程序员秘密

*与《redhat5.5 x64 静默安装oracle 11g》这篇文章所不同的是,这里是分步安装: 1.安装11g r2软件 2.创建数据库 3.安装监听*redhat5.5 x64 (rhel5.5 x64)oracle 11.2.0.3 x64一.准备工作“#”后跟命令表示以操作系统下root用户操作; “$”后跟命令表示以操作系统下Oracle用户操作; 下载软件包 官网

JSAPI微信支付返回错误:fail_no permission to execute_weixin_30689307的博客-程序员秘密

问题描述fail_no permission to execute 一定是授权目录出问题了,因为没有权限。开发环境及可能造成的原因这次的微信开发是用的Mvc4,支付的封装代码不会有问题(用过很多次),授权目录和其他设置已配置好。我习惯的写链接地址是这样的:/u/RechargeUrl_WXPay/?showwxpaytitle=1标准的写法是这样的:{controlle...

PHP 安全编程建议_weixin_33693070的博客-程序员秘密

简介要提供互联网服务,当你在开发代码的时候必须时刻保持安全意识。可能大部分 PHP 脚本都对安全问题都不在意,这很大程度上是因为有大量的无经验程序员在使用这门语言。但是,没有理由让你因为对你的代码的不确定性而导致不一致的安全策略。当你在服务器上放任何涉及到钱的东西时,就有可能会有人尝试破解它。创建一个论坛程序或者任何形式的购物车,被攻击的可能性就上...

从矩阵分解到FM的演进、FM如何用于召回和排序以及实现说明_文文学霸的博客-程序员秘密

本文主要包含以下内容:1.背景2.矩阵分解类算法2.1 协同过滤 CF2.2 矩阵分解 MF2.3 逻辑回归 LR2.4 因子分解机FM2.5 组合模型3.FM深入介绍3.1 FM特点3....

随便推点

【Unity Shaders】Surface Shader 概述_unity没有surface option_不负初心的博客-程序员秘密

Shader 初识         Surface Shaders: 表面着色器,可以适用很多情况下,去除了很多底层工作         Fragment Shaders: 片段着色器,可以做一些底层工作,比如顶点光照,这对于移动设备和多个通道(passes)所必须的更高级效果会非常有用   Shader 资源         几个重量级的shaders 参考网址:         M

java驼峰转下划线_Longerandlonger的博客-程序员秘密

Pattern pattern = Pattern.compile("\\B(\\p{Upper})(\\p{Lower}*)");Matcher matcher = pattern.matcher("longAndLongCity");String replaceAll = matcher.replaceAll("_$1$2");String result = replaceAll.toLo

弹框提醒_weixin_30347335的博客-程序员秘密

&lt;button id="alert_alertBtn" type="button" style="display:none" class="am-btn am-btn-primary" data-am-modal="{target: '#my-alert'}"&gt; Alert&lt;/button&gt;&lt;div class="am-modal am-modal-ale...

百度惊雷算法3.0即将上线 是时候要给网站安排SEO顾问了!_科猫网的博客-程序员秘密

2021年1月12日,百度搜索资源平台发布了一篇《惊雷算法3.0即将上线 持续打击刷点击作弊行为》的文章,对于很多利用快排进行排名的站长而言,无疑是一记重锤!百度宣布,本次升级严厉打击通过伪造用户行为来试图提升网站搜索排序的作弊行为!本次算法有四个主要升级点:1、加强了对作弊行为的识别;2、加大了对作弊站点的打击力度;3、扩大了算法的覆盖范围;4、对违规行为较严重的领域(如:汽车、下载、招聘、B2B、网站SEO等)进行了针对性的打击。百度表示,请存在问题的站点尽快自查整改。鼓励开发者通过生产

[简单]ora01899精度说明符错误问题解决记录_oy538730875的博客-程序员秘密

       同事写了段sql报错,错误信息:ora-01899精度说明符错误,如下:                同事的sql简化后如下:       select 1 from dual where sysdate &amp;gt; trunc(to_date('2014-01-06', 'yyyy-mm-dd'), ' yyyy')       错误原因:    ...

Tensorflow向量加tf.add_tf向量相加_rosefunR的博客-程序员秘密

import tensorflow as tf# Press Shift+F10 to execute it or replace it with your code.# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.def element_add(a, b): return tf.add(a, b)def main(): vara =