• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            tianxuyuan

            待做

            Sorting by Swapping
            Time Limit:1000MS  Memory Limit:10000K
            Total Submit:2515 Accepted:1374

            Description
            Given a permutation of numbers from 1 to n, we can always get the sequence 1, 2, 3, ..., n by swapping pairs of numbers. For example, if the initial sequence is 2, 3, 5, 4, 1, we can sort them in the following way:

            2 3 5 4 1
            1 3 5 4 2
            1 3 2 4 5
            1 2 3 4 5

            Here three swaps have been used. The problem is, given a specific permutation, how many swaps we needs to take at least.


            Input
            The first line contains a single integer t (1 <= t <= 20) that indicates the number of test cases. Then follow the t cases. Each case contains two lines. The first line contains the integer n (1 <= n <= 10000), and the second line gives the initial permutation.

            Output
            For each test case, the output will be only one integer, which is the least number of swaps needed to get the sequence 1, 2, 3, ..., n from the initial permutation.

            Sample Input


            2
            3
            1 2 3
            5
            2 3 5 4 1

            Sample Output


            0
            3

            posted on 2007-05-05 11:17 田旭園 閱讀(621) 評論(1)  編輯 收藏 引用

            Feedback

            # re: 待做 2007-05-06 10:28 Santa

            其實就是求解逆序數的個數……
            O(nlogn)時間內可以完成,參考歸并排序……  回復  更多評論   

            麻豆久久| 久久精品国产精品亚洲人人| 国产亚洲美女精品久久久2020| 久久精品一区二区三区AV| 久久99国产综合精品免费| 国内精品久久久久久麻豆 | 91精品国产综合久久精品| 久久婷婷久久一区二区三区| 欧美午夜精品久久久久久浪潮| 亚洲国产欧美国产综合久久| 国产精品一区二区久久精品无码| 亚洲国产精品成人久久| 久久亚洲高清综合| 久久精品国产精品亚洲精品 | 久久无码AV一区二区三区| 91久久婷婷国产综合精品青草| 伊人久久国产免费观看视频| 久久青青草原精品影院| 日本久久久久亚洲中字幕| 久久无码高潮喷水| 亚洲精品高清一二区久久| 欧美国产精品久久高清| 91精品免费久久久久久久久| 丁香狠狠色婷婷久久综合| 人妻无码αv中文字幕久久| 国产精品成人久久久| 亚洲精品成人久久久| 久久中文字幕视频、最近更新| A级毛片无码久久精品免费| 日本久久久精品中文字幕| 精品综合久久久久久97超人 | 亚洲人成无码www久久久| 2020最新久久久视精品爱| 国产精品美女久久久久| 国产成年无码久久久久毛片 | 久久精品女人天堂AV麻| 国产精品熟女福利久久AV| 国产精品热久久无码av| 久久久亚洲精品蜜桃臀| 无码8090精品久久一区 | 国产A级毛片久久久精品毛片|