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;
}

原文地址:https://www.cnblogs.com/EDGsheryl/p/9157964.html

时间: 2024-10-15 05:06:43

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

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

Codeforces Round #485 (Div. 2) D. Fair 题目连接: http://codeforces.com/contest/987/problem/D Description Some company is going to hold a fair in Byteland. There are $n$ towns in Byteland and $m$ two-way roads between towns. Of course, you can reach any t

Codeforces Round #485 (Div. 2)

A. Infinity Gauntlet time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You took a peek on Thanos wearing Infinity Gauntlet. In the Gauntlet there is a place for six Infinity Gems: the Power G

【思维】Codeforces Round #485 (Div. 2) B. High School: Become Human(对数)

题目链接:http://codeforces.com/contest/987/problem/B 在运算的时候取对数就好了 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define ll long long 6 #define eps 1e-6 7 8 int main() 9 { 10 ll x,y; 11 double xx; 12 scanf("%lld %lld",&x,&y);

Codeforces Round #485 Div. 1 vp记

A:对每种商品多源bfs一下每个点到该商品的最近距离,对每个点sort一下取前s个即可. #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define int long long #d

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

C. Three displays It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk (a town in Zabaikalsky region), wants to rent three displays to highlight an important problem. There are nn displays placed along a road, and the ii-th

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 standard input output standard output Year 2118. Androids are in mass production for decades now, and they do all the work for humans. But androids ha

Codeforces Round #631 (Div. 2) B. Dreamoon Likes Permutations(排列组合)

The sequence of mm integers is called the permutation if it contains all integers from 11 to mm exactly once. The number mm is called the length of the permutation. Dreamoon has two permutations p1p1 and p2p2 of non-zero lengths l1l1 and l2l2 . Now D

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿