• 【解题报告】POJ2299 超快速排序


    【解题报告】POJ2299 超快速排序

    题目:超快速排序(已翻译)

    解题思路:

    归并排序求逆序对

    归并排序使我们众所周知的,我们只要在归并排序中计算每一个子序列中的逆序对数,我们就可以计算出总的逆序对数了,也就是

    [cnt+=mid-i+1 ]

    然后就完成了这道题

    AC代码

    #include <iostream>
    #include <cstring>
    using namespace std;
    int n;
    long long cnt;
    const int maxn=500005;
    long long a[maxn];
    long long b[maxn];
    void merge_sort(int l,int r)
    {
        if(r>l)
        {
            int mid=(l+r)/2;
            int i=l; 
            int p=l,q=mid+1;
            merge_sort(l,mid);
            merge_sort(mid+1,r);
            while(p<=mid||q<=r)
            {
                if(q>r||(p<=mid&&a[p]<=a[q]))
                b[i++] = a[p++];
                else
                {
                    b[i++]=a[q++];
                    cnt+=mid-p+1;
                }
            }
            for(i=l;i<=r;i++)
            a[i]=b[i];
        }
    }
    int main()
    {
    	while(cin>>n&&n)
    	{
    		for(int i=1;i<=n;i++)
    		cin>>a[i];
    		cnt=0;
    		merge_sort(1,n);
    		cout<<cnt<<endl;
    	}
    	return 0;
    }
    

    顺便附上归并排序的代码

    #include <iostream>
    using namespace std;
    int n,cnt;
    const int maxn=100005;
    long long a[maxn];
    long long b[maxn];
    void merge_sort(int l,int r)
    {
        if(r>l)
        {
            int mid=(l+r)/2;
            int i=l; 
            int p=l,q=mid+1;
            merge_sort(l,mid);
            merge_sort(mid+1,r);
            while(p<=mid||q<=r)
            {
                if(q>r||(p<=mid&&a[p]<=a[q]))
                b[i++] = a[p++];
                else
                {
                    b[i++]=a[q++];
                    cnt+=mid-p+1;
                }
            }
            for(i=l;i<=r;i++)
            a[i]=b[i];
        }
    }
    int main()
    {
    	cin>>n;
    	for(int i=1;i<=n;i++)
    	cin>>a[i];
    	merge_sort(1,n);
    	for(int i=1;i<=n;i++)
    	{
    		cout<<a[i]<<" ";
    	}
    	cout<<endl;
    	return 0;
    }
    
    
    本博文为wweiyi原创,若想转载请联系作者,qq:2844938982
  • 相关阅读:
    [LeetCode] Wiggle Sort
    [LeetCode] Perfect Squares
    [LeetCode] Minimum Window Substring
    [LeetCode] Valid Sudoku
    [LeetCode] Sudoku Solver
    [LeetCode] First Bad Version
    [LeetCode] Find the Celebrity
    [LeetCode] Paint Fence
    [LeetCode] H-Index II
    [LeetCode] H-Index
  • 原文地址:https://www.cnblogs.com/wweiyi2004/p/11318693.html
Copyright © 2020-2023  润新知