zoj 2386 - Ultra-QuickSort

题目:只允许交换相邻元素的排序,统计将最小交换次数。

分析:分治,逆序数。在合并排序的过程中进行逆序对的求解。

合并A,B两个字串时利用两根指针作为计数;

当B中元素放入新数组时A中所剩元素一定大于B;

每次计数加和即可。

说明:置换群 可以用来计算任意最小交换。。。 (2011-09-20 14:25)

#include <stdio.h>
#include <stdlib.h>

int Data[ 500005 ];
int Save[ 500005 ];

long long MergeSort( int a, int b )
{
    if ( a < b ) {
        long long L = MergeSort( a, (a+b)/2 );
        long long R = MergeSort( (a+b)/2+1, b );
        //Merge
        long long Count = L+R;
        int ps = a,pe = (a+b)/2;
        int qs = (a+b)/2+1,qe = b;
        int move = a;
        while ( ps <= pe || qs <= qe )
            if ( qs <= qe && ( ps == pe+1 || Data[ ps ] > Data[ qs ] ) ) {
                Save[ move ++ ] = Data[ qs ++ ];
                Count += pe-ps+1LL;//计算A B 中的逆序数
            }else
                Save[ move ++ ] = Data[ ps ++ ];

        for ( int i = a ; i <= b ; ++ i )
            Data[ i ] = Save[ i ];

        return Count;
    }else return 0LL;
} 

int main()
{
    int n;
    while ( scanf("%d",&n) && n ) {
        for ( int i = 1 ; i <= n ; ++ i )
            scanf("%d",&Data[ i ]);
        printf("%lld\n",MergeSort( 1, n ));
    }
    return 0;
}
时间: 2024-08-09 06:32:31

zoj 2386 - Ultra-QuickSort的相关文章

poj-2299 Ultra—QuickSort(归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 38688   Accepted: 13950 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

归并排序求逆序对模板(未完待续)

归并排序求逆序对题目(持续更新) \(1.\) \(Ultra\) \(Quicksort\) (需要该篇博文的阅读密码) 归并排序求逆序对 细节:传参三个,左.中.右三端点,每次运算注意中端点总取左右端点和的一半:返回条件为左右端点相等,此时无需排序. \(View\) \(Code\) void msort(int l,int mid,int r) { if(l==r) return; msort(l,(l+mid)>>1,mid); msort(mid+1,(r+mid+1)>&g

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

ZOJ 3607 Lazier Salesgirl (贪心)

Lazier Salesgirl Time Limit: 2 Seconds      Memory Limit: 65536 KB Kochiya Sanae is a lazy girl who makes and sells bread. She is an expert at bread making and selling. She can sell the i-th customer a piece of bread for price pi. But she is so lazy

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

ZOJ - 2243 - Binary Search Heap Construction

先上题目: Binary Search Heap Construction Time Limit: 5 Seconds      Memory Limit: 32768 KB Read the statement of problem G for the definitions concerning trees. In the following we define the basic terminology of heaps. A heap is a tree whose internal n

ZOJ 2859 二维线段树

思路:自己写的第二发二维线段树1A,哈哈,看来对二维的push操作比较了解了:但是还没遇到在两个线段树中同时进行push操作的,其实这题我是想在x维和y维同时进行push操作的,但是想了好久不会,然后看到这题又给出10秒,然后想想在x维线段直接单点查询肯定也过了,然后在第二维就只有pushup操作,在第一维线段树没有pushup操作.要是在第一维也有pushup操作的话,那就不用单点查询那么慢了.不过也A了,想找题即在二维同时进行pushup和pushdown操作的. #include<iost