• <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)時間內可以完成,參考歸并排序……  回復  更多評論   

            久久亚洲精品无码aⅴ大香| 97久久精品无码一区二区天美| 99久久国产亚洲高清观看2024| 日韩欧美亚洲综合久久影院Ds | 99re这里只有精品热久久| 97精品伊人久久久大香线蕉 | 久久精品国产免费观看三人同眠| 久久精品国产清高在天天线| 国产免费久久精品99久久| 99精品国产99久久久久久97| 97精品国产97久久久久久免费| 国产成人精品久久| 日产久久强奸免费的看| 日本一区精品久久久久影院| 久久婷婷五月综合97色直播| 久久99久久无码毛片一区二区 | 九九99精品久久久久久| 久久中文字幕人妻丝袜| 国产综合免费精品久久久| AV无码久久久久不卡蜜桃| 日韩va亚洲va欧美va久久| 久久青青草原国产精品免费| 久久国产欧美日韩精品| 久久精品aⅴ无码中文字字幕不卡| 激情五月综合综合久久69| 99久久免费国产特黄| 亚洲AV无码久久寂寞少妇| 欧美日韩精品久久免费| 亚洲婷婷国产精品电影人久久| 欧美日韩中文字幕久久伊人| 伊人久久综合热线大杳蕉下载| 精品久久久久久久久午夜福利| 精品久久久久香蕉网| 日韩精品久久久肉伦网站 | 亚洲国产精品人久久| 欧美日韩中文字幕久久伊人| 麻豆精品久久精品色综合| 国产精品免费看久久久香蕉| 久久伊人色| 亚洲中文字幕无码久久精品1| 日韩乱码人妻无码中文字幕久久 |