zoukankan      html  css  js  c++  java
  • Codeforces Round #655 (Div. 2) C. Omkar and Baseball(思维)

    Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across nn sessions follow the identity permutation (ie. in the first game he scores 11 point, in the second game he scores 22 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up!

    Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3][1,2,3] can yield [3,1,2][3,1,2] but it cannot yield [3,2,1][3,2,1] since the 22 is in the same position.

    Given a permutation of nn integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed 10181018 .

    An array aa is a subarray of an array bb if aa can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

    Input

    Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1001≤t≤100 ). Description of the test cases follows.

    The first line of each test case contains integer nn (1≤n≤2⋅1051≤n≤2⋅105 )  — the length of the given permutation.

    The second line of each test case contains nn integers a1,a2,...,ana1,a2,...,an (1≤ain1≤ai≤n )  — the initial permutation.

    It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105 .

    Output

    For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.

    Example

    Input

    Copy

    2

    5

    1 2 3 4 5

    7

    3 2 4 5 1 6 7

    Output

    Copy

    0

    2

    思维题杀我,看了题解恍然大悟T^T

    以后看到这种有点摸不着头脑的操作,又问最小次数,可能就是0,1,2次这样…

    分为三种情况,已经有序,仅有一段完全无序(a[i] != i)以及其他情况。有序的话输出0,仅一段完全无序输出1,这些都很简单。关键是其他情况。其他情况仅需两次。第一次将所有大于n/2的数分到前半段,小于等于n/2的数分到后半段,这样是可以满足条件的(即使有的大于n/2的数已经在前半段了);第二次将所有数排序即可,此时所有数都不会在原来的位置,也满足条件。

    #include <bits/stdc++.h>
    using namespace std;
    int n;
    int a[200005];
    int main()
    {
        int t;
        cin >> t;
        while(t--)
        {
            cin >> n;
            for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
            int flag = 1;
            int pos1 = 0, pos2 = 0, cnt = 0;
            for(int i = 1; i <= n; i++)
            {
                if(i == a[i]) cnt++;
                else 
                {
                    pos1 = i; break;
                }
            }
            if(cnt != n)
            {
                for(int i = n; i >= 1; i--)
                {
                    if(i != a[i])
                    {
                        pos2 = i;
                        break;
                    }
                }
            }
            if(cnt == n)
            {
                cout << 0 << endl;
                continue;
            }
            for(int i = pos1; i <= pos2; i++)
            {
                if(a[i] == i)
                {
                    flag = 2;
                }
            }
            cout << flag << endl;
            
        }
    }
  • 相关阅读:
    大数减法模板
    扩展kmp模板
    poj2185(kmp)
    poj3167(kmp)
    kuangbin专题K(next数组)
    kuangbin专题16I(kmp)
    kuangbin专题16H(next数组)
    kuangbin专题16D(next求最小循环节)
    kuangbin专题16B(kmp模板)
    Java集合--TreeSet
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/13293489.html
Copyright © 2011-2022 走看看