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

            日日狠狠久久偷偷色综合0| 激情伊人五月天久久综合| 内射无码专区久久亚洲| 狠狠综合久久AV一区二区三区| 亚洲精品无码久久久影院相关影片| 久久er热视频在这里精品| 日韩中文久久| 国产精品久久自在自线观看| 97香蕉久久夜色精品国产| 91精品国产91久久久久久蜜臀| 亚洲v国产v天堂a无码久久| 国产精品久久成人影院| 怡红院日本一道日本久久 | 综合网日日天干夜夜久久| 国产美女久久精品香蕉69| 日韩久久无码免费毛片软件| 久久久久AV综合网成人 | 欧美精品福利视频一区二区三区久久久精品 | 无码精品久久一区二区三区| 99久久国语露脸精品国产| 久久婷婷五月综合成人D啪| 国产精品熟女福利久久AV| 7777久久亚洲中文字幕| 午夜欧美精品久久久久久久| 国产精品成人久久久| 人妻精品久久久久中文字幕| 国产精品99久久久久久董美香| 久久精品中文闷骚内射| 亚洲色欲久久久综合网东京热| 一本久久精品一区二区| 亚洲精品无码久久毛片| 久久精品国产99久久久香蕉| 国产日产久久高清欧美一区| 日韩AV无码久久一区二区| 7777精品久久久大香线蕉| 伊人久久大香线蕉AV色婷婷色| 国产69精品久久久久久人妻精品| 伊人色综合久久天天网| 国产亚洲美女精品久久久2020| 久久婷婷国产剧情内射白浆| 无码伊人66久久大杳蕉网站谷歌|