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的解法 ...
随机推荐
- Interpreting the genomic landscape of speciation: a road map for finding barriers to gene flow
1.摘要 物种形成是种群间生殖隔离的进化过程,是连续的.复杂的,涉及多个相互作用的屏障.在完成之前,这一过程的影响会随着基因组的不同而变化,并可能导致具有分化和分化波峰和波谷的异质性基因组景观.当基因 ...
- windows yii2 配置redis
1 安装redis https://blog.csdn.net/gaotanpan3666/article/details/83047081 安装redis 2 yii安装redis扩展 compo ...
- 使用autohotkey修改方向键、回车和启动程序
具体步骤 下载并安装autohotkey. 在你觉得合适的地方鼠标右键-新建-autohotkey script(脚本):或者创建一个别的文件,再把后缀改成ahk也可以 一个新建的ahk文档里面会有这 ...
- mysql判断表里面一个逗号分隔的字符串是否包含单个字符串、查询结果用逗号分隔
1.mysql判断表里面一个逗号分隔的字符串是否包含单个字符串 : FIND_IN_SET select * from tablename where FIND_IN_SET(传的参数,匹配字段) 例 ...
- 搭建vue脚手架
1.Node.js安装 1.1下载安装 在node.js 官网下载, 根据自己电脑系统安装,一直点下一步即可 1.2测试安装是否成功 Windows+R打开cmd窗口,输入node -v回车出现版本号 ...
- Distance on the tree
Distance on the tree https://nanti.jisuanke.com/t/38229 DSM(Data Structure Master) once learned abou ...
- 项目总结21:项目总结21:input实现多图上传(FormData)(上传OSS并保存数据库)
项目总结21:input实现多图上传(FormData)(上传OSS并保存数据库) 备注:本案例,作为Demo,包含少量的项目业务逻辑,input多图上传的逻辑是完整的: 不废话直接上代码 1-前端标 ...
- UEFI EVENT 全解
Event和Timer在UEFI当中是怎么实现的以及原理,我们先从Timer开始,然后细细的拨开隐藏在底层的实现. 先说Timer,那什么是Timer呢?其实在中文里面我们把它叫做定时/计数器,但是我 ...
- grid布局
display: grid; //行 //每一行中有几块,每块所占的width的百分比(1) grid-template-columns: 70% 30%; //每个div的width 100px,自 ...
- 2018-2019-2 20165315 《网络对抗技术》Exp4 恶意代码分析
2018-2019-2 20165315 <网络对抗技术>Exp4 恶意代码分析 一.实验要求 1.系统运行监控 使用如计划任务,每隔一分钟记录自己的电脑有哪些程序在联网,连接的外部IP是 ...