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

            狠狠色丁香久久综合五月| 久久精品国产色蜜蜜麻豆| 国产精品成人99久久久久91gav| 国产精品xxxx国产喷水亚洲国产精品无码久久一区| 亚洲国产精品久久久久久| 中文字幕精品久久| 精品熟女少妇av免费久久| 久久精品国产一区二区三区| 久久亚洲AV成人无码电影| 久久精品国产欧美日韩| 国产午夜福利精品久久2021 | 波多野结衣久久一区二区| 亚洲级αV无码毛片久久精品| 国产呻吟久久久久久久92| 人妻无码久久一区二区三区免费| 久久久久亚洲AV成人网人人软件| 久久久久亚洲精品天堂| 国内精品伊人久久久久妇| 国内精品久久久久久中文字幕| 久久香蕉超碰97国产精品| 色诱久久av| 久久中文精品无码中文字幕 | 伊人情人综合成人久久网小说| 久久er国产精品免费观看2| 人妻无码中文久久久久专区| 7777精品久久久大香线蕉| 亚洲精品WWW久久久久久| 久久精品国产第一区二区| 99久久99久久精品国产片| 日本免费一区二区久久人人澡 | 日韩精品久久久肉伦网站 | 国产99久久久国产精品小说| 久久免费视频一区| 亚洲午夜无码久久久久小说 | 91精品免费久久久久久久久| 国产一区二区三区久久| 国内精品久久九九国产精品| 中文字幕亚洲综合久久| 久久国产精品无码网站| 日本高清无卡码一区二区久久 | 久久九九青青国产精品|