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

            国产精品一区二区久久精品| AV无码久久久久不卡网站下载| 亚洲国产一成人久久精品| 中文字幕亚洲综合久久菠萝蜜| 精品熟女少妇aⅴ免费久久| 97精品伊人久久久大香线蕉| 亚洲伊人久久大香线蕉苏妲己| 久久国产精品99久久久久久老狼 | 日韩精品久久久久久| 久久99精品久久久久子伦| 国产高潮国产高潮久久久| 国产精品丝袜久久久久久不卡| 久久精品亚洲欧美日韩久久| 久久久久久国产精品美女| 免费观看久久精彩视频| 国产精品99久久久久久董美香| 日批日出水久久亚洲精品tv| 久久久久久国产精品美女| 丰满少妇高潮惨叫久久久| 日日狠狠久久偷偷色综合96蜜桃| 国产激情久久久久久熟女老人| 国产精品久久久久久| 久久天天躁夜夜躁狠狠| 久久精品国产免费一区| 中文字幕精品无码久久久久久3D日动漫| 国内精品伊人久久久久777| 国产99精品久久| 伊人久久精品无码av一区| 久久se这里只有精品| 久久国产一区二区| 国内高清久久久久久| 国产—久久香蕉国产线看观看| 色偷偷88888欧美精品久久久| 精品国产综合区久久久久久 | 久久国产精品77777| 亚洲精品NV久久久久久久久久| 久久91精品久久91综合| 久久午夜无码鲁丝片| 久久夜色精品国产亚洲| 久久久久这里只有精品| 久久99精品久久久久久9蜜桃 |