Codeforces Round #485 (Div. 2) E. Petr and Permutations
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的更多相关文章
- Codeforces Round #485 (Div. 2)
Codeforces Round #485 (Div. 2) https://codeforces.com/contest/987 A #include<bits/stdc++.h> us ...
- Codeforces Round #485 (Div. 2) D. Fair
Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #485 (Div. 2) C. Three displays
Codeforces Round #485 (Div. 2) C. Three displays 题目连接: http://codeforces.com/contest/987/problem/C D ...
- Codeforces Round #485 (Div. 2) A. Infinity Gauntlet
Codeforces Round #485 (Div. 2) A. Infinity Gauntlet 题目连接: http://codeforces.com/contest/987/problem/ ...
- Codeforces Round #485 Div. 1 vp记
A:对每种商品多源bfs一下每个点到该商品的最近距离,对每个点sort一下取前s个即可. #include<iostream> #include<cstdio> #includ ...
- 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 ...
- Codeforces Round #485 (Div. 2) C题求三元组(思维)
C. Three displays time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- Codeforces Round #264 (Div. 2) D. Gargari and Permutations 多序列LIS+dp好题
http://codeforces.com/contest/463/problem/D 求k个序列的最长公共子序列. k<=5 肯定 不能直接LCS 网上题解全是图论解法...我就来个dp的解法 ...
随机推荐
- Spring Boot与Docker部署
开启Docker远程访问 首先需要开启docker远程访问功能,以便可以进行远程操作. CentOS 6 修改/etc/default/docker文件,重启后生效(service docker re ...
- Day08 - Ruby比一比:String的+=与concat串接
前情提要: 在第七天我们透过比较Symbol和String,发现字串比符号多了更多方法!为了活用string method,今天我们接续前文,来探讨一题跟字串有关的题目: Ruby经典面试题目#08( ...
- Java属性中指定Json的属性名称
只需要使用注解"@JsonProperty(value = "pwd")" import com.fasterxml.jackson.annotation.Js ...
- python中文件处理--判断文件读取结束方法
一.readline函数 按行遍历读取文件的方法,通过这个方法,readline() 每次只读取一行,通常比 .readlines() 慢得多.仅当没有足够内存可以一次读取整个文件时,才应该使用 .r ...
- mysql中查询的优先级
sql和mysql执行顺序,内部机制是一样的,最大的区别在别名上 一.sql执行顺序 1.from 2.on 3.join 4.where 5.group by(开始使用select中的别名,后面 的 ...
- ASCII,Unicode,GBK和UTF-8字符编码的区别和联系
如果经常写python2,肯定会遇到各种“奇怪”的字符编码问题,每次都通过谷歌解决了,但是为什么会造成这种乱码.decode/encode失败等等,本文就字符和字符编码做一个总结,更加清晰区分诸多的编 ...
- Linux nfs使用krb5的方式安全挂载
配置安全的网络nfs文件共享服务 由于本人是使用的rhce模拟考试环境来做的本题目,所以文中说到的实验脚本和评分脚本,以及krb5.keytab文件只有我本套环境独有,如果自己做练习可以不去使用实验脚 ...
- 关于AVL树的思考
AVL树即平衡二叉树,每个结点有一个平衡因子,即左子树高度减去右子树高.每插入一个结点时,从根部开始按二叉排序树的方法,与节点不断比较,按大小向左右子树插入.在与最后的节点比较后插入时,若有兄弟节点, ...
- 20175126Apollo 20175126《Java程序设计》结队编程项目——四则运算
结队编程项目——四则运算 一.项目需求 自动生成小学四则运算题目(加.减.乘.除)统计正确率 支持整数 支持多运算符(比如生成包含100个运算符的题目) 支持真分数 需求分析: 生成四则运算:需要使用 ...
- leetcode 字谜
242. Valid Anagram Easy 66298FavoriteShare Given two strings s and t , write a function to determine ...