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

            久久久免费观成人影院 | 亚洲国产精品无码久久98| 色天使久久综合网天天| 精品国产乱码久久久久软件| 午夜精品久久久久久毛片| 国产精品欧美亚洲韩国日本久久 | 久久精品国产99国产精品澳门| 精品九九久久国内精品| 色综合久久天天综线观看| 久久久精品国产sm调教网站| 久久精品无码一区二区三区日韩| 久久久一本精品99久久精品88| 九九99精品久久久久久| 无码八A片人妻少妇久久| 日本精品久久久中文字幕| 久久久亚洲裙底偷窥综合| 狠狠色伊人久久精品综合网 | 热久久这里只有精品| 久久青青草原精品国产| 狠狠色丁香久久婷婷综合_中 | 久久人人妻人人爽人人爽| 久久久精品人妻无码专区不卡| 久久99国产综合精品免费| 日韩人妻无码一区二区三区久久99| 国产成人精品久久亚洲| 久久国产精品久久国产精品| 狠狠88综合久久久久综合网 | 久久九九有精品国产23百花影院| 国产精品亚洲综合久久| 精品人妻伦九区久久AAA片69| 久久91精品久久91综合| 日韩久久久久久中文人妻| 久久久久久久女国产乱让韩| 伊人情人综合成人久久网小说| 亚洲精品无码久久久久AV麻豆| 久久久黄片| 人人狠狠综合久久亚洲| 亚洲国产高清精品线久久| 亚州日韩精品专区久久久| 无码人妻久久一区二区三区蜜桃 | 欧洲性大片xxxxx久久久|