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

题目连接:

http://codeforces.com/contest/987/problem/E

Description

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$ to $n$ and then $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$ times instead of $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.

Sample Input

5
2 4 5 1 3

Sample Output

Petr

题意

给定一个排列,问它是从\(1, 2, 3,...,n\)变换\(3n\)次而来,还是\(7n+1\)

Give a random permutation, it maybe transform \(3n\) times or \(7n+1\) times from the permutation of numbers from \(1\) to n. Judging it is \(3n\) or \(7n+1\) ?

题解:

考虑n是奇数的情况,\(3n\)为奇数,\(7n+1\)为偶数;n是偶数时,\(3n\)为偶数,\(7n+1\)为奇数。

考虑最小交换次数,假定为k,\(k+2*t\)次交换都可以变成目标串,但奇偶性相同。所以只需判断最小交换次数的奇偶性即可。

When n is a odd number, \(3n\) is odd,\(7n+1\) is even.When n is a even number, \(3n\) is even,\(7n+1\) is odd.

Consider about the swap times, assume k is the mininum swap times to the final permutation , \(k+2*t\) will reach the final result , \(k\) and \(k+2*t\) have the same parity. So we need to know the parity of the mininum swap times.

如果我们不把stdio的同步关闭,将会读入超时

If we dont close the sync with stdio , time limit exceeded.

代码

#include <bits/stdc++.h>

using namespace std;

int n;
map<int,int> m;
int a[1000010];
int c[1000010];
int ans;
inline int lowbit(int k) {
return (k&(-k));
} void update(int t) {
while (t<n) {
c[t]++;
t+=lowbit(t);
}
} int sum(int t) {
int ret=0;
while (t>0) {
ret+=c[t];
t-=lowbit(t);
}
return ret;
} int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr); cin>>n;
for (int i=1;i<=n;i++) {
cin>>a[i];
m[a[i]]=i;
}
for (int i=n;i;i--) {
ans += i-(m[i]-sum(m[i]-1));
update(m[i]);
}
if ((n&1)^(ans&1)==0) cout << "Petr" <<endl;
else cout << "Um_nik" <<endl;
}

Codeforces Round #485 (Div. 2) E. Petr and Permutations的更多相关文章

  1. Codeforces Round #485 (Div. 2)

    Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...

  2. Codeforces Round #485 (Div. 2) D. Fair

    Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...

  3. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  4. Codeforces Round #485 (Div. 2) C. Three displays

    Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...

  5. Codeforces Round #485 (Div. 2) A. Infinity Gauntlet

    Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...

  6. Codeforces Round #485 Div. 1 vp记

    A:对每种商品多源bfs一下每个点到该商品的最近距离,对每个点sort一下取前s个即可. #include<iostream> #include<cstdio> #includ ...

  7. Codeforces Round #485 (Div. 2)-B-High School: Become Human

    B. High School: Become Human time limit per test 1 second memory limit per test 256 megabytes input ...

  8. Codeforces Round #485 (Div. 2) C题求三元组(思维)

    C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题

    http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...

随机推荐

  1. CS通用项目系统搭建——三层架构第一天

    CS通用项目:使用三层架构进行搭建 三层架构: 表现层(UI(User Interface)):展示给用户的层面,包含窗体控件数据等信息. 业务逻辑层(BLL(Business Logic Layer ...

  2. servlet实现mysql数据库分页

    一.分页所需要的sql语句准备 select * from table limit m,n其中m是指记录开始的index,从0开始,表示第一条记录n是指从第m+1条开始,取n条. 例如:select ...

  3. css入门第一天

    一丶Web标准Web标准不是某一个标准,而是一系列标准的集合,内容与结构与表现形式的分离网页主要有四个部分组成:内容(content),结构(structure), 表现(presentation)和 ...

  4. scrapy 之自定义命令运行所有爬虫文件

    1.在spider文件夹同级目录创建commands python包 2.在包下创建command.py文件 3.从scrapy.commands包下引入ScrapyCommand 4.创建一个类,继 ...

  5. 5. Go函数

    [定义函数] 直接上一个栗子,Go语言定义函数: func add(a int, b int) int { return a + b } 一目了然,还不太习惯Go语言的命名方式, 类型为什么要写到后面 ...

  6. <web-view>中JSSDK

    如果只是使用wx.miniProgram.navigateTo这种导航的接口,jssdk可以不用做配置,引用js后直接使用就行,如果chooseImage这种,就需要获取配置了,步骤如下: 先在后端通 ...

  7. P3865 【模板】ST表

    P3865 [模板]ST表 https://www.luogu.org/problemnew/show/P3865 题目背景 这是一道ST表经典题——静态区间最大值 请注意最大数据时限只有0.8s,数 ...

  8. Cisco交换机设置备份

    conf tusername xa privilege 3 secret xxx aaa new-modelaaa authentication login default local enablea ...

  9. Spring Boot HikariCP 一 ——集成多数据源

    其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...

  10. jqgrid修改表格内容为居中

    看了手册没有发现自带的方法,所以使用了自定义css <style> #tableDataSearch tr td{ text-align:center; } </style>