• <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 田旭園 閱讀(615) 評論(1)  編輯 收藏 引用

            Feedback

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

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

            久久精品天天中文字幕人妻| 久久精品这里只有精99品| 国产69精品久久久久观看软件| 一本久久综合亚洲鲁鲁五月天| 日产精品99久久久久久| 久久se这里只有精品| 久久久久亚洲AV成人网人人网站| 久久综合狠狠综合久久| 久久久久久久国产免费看| 一本色道久久HEZYO无码| 91亚洲国产成人久久精品| 久久香蕉国产线看观看精品yw| 99久久国产综合精品网成人影院 | 国产精品久久亚洲不卡动漫| 亚洲狠狠久久综合一区77777| 久久精品国产99国产精品导航| 久久综合久久综合久久综合| 亚洲色欲久久久综合网东京热| 久久精品国产欧美日韩| 久久国产精品久久精品国产| 国产成人久久精品一区二区三区| 91精品国产色综久久 | 国产成人久久久精品二区三区 | 国产综合免费精品久久久| 久久久亚洲欧洲日产国码二区| 欧美午夜A∨大片久久 | 午夜天堂av天堂久久久| 久久久精品久久久久久| 青青草国产精品久久久久| 久久精品国产清高在天天线| 久久精品一本到99热免费| 欧美日韩成人精品久久久免费看| 久久久WWW成人免费精品| 久久国产一区二区| 国产亚洲欧美成人久久片 | 国产精品99久久久久久董美香 | 久久亚洲高清观看| 久久99精品久久久久久hb无码 | 热久久国产精品| 国产亚洲精久久久久久无码AV| 国产精品午夜久久|