poj 2299 求逆序数

http://poj.org/problem?id=2299

#include<iostream>
using namespace std;
int aa[500010],bb[500010];
long long  s=0;
void merge(int l,int m,int r)
{
     int i=l,j=m+1,t=0;
     while(i<=m&&j<=r)
     {
          if(aa[i]>aa[j])
          {
               bb[t++]=aa[j++];
               s+=m-i+1;
          }
          else
          {
               bb[t++]=aa[i++];
          }
     }
     while(i<=m)
          bb[t++]=aa[i++];
     while(j<=r)
          bb[t++]=aa[j++];
     for(int i=0;i<t;i++)
     {
          aa[l+i]=bb[i];
     }
}
void Msort (int L,int R)
{
    int cen;
    if(L<R)
    { cen=(L+R)/2;
    Msort(L,cen);
    Msort(cen+1,R);
    merge(L,cen,R);

    }

}
void merge_sort(int *a,int n)
{ Msort(0,n-1);

}
int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0)break;

    for(int i=0;i<n;i++)
    {
        cin>>aa[i];
    }
    merge_sort(aa,n);

    cout<<s<<endl;
     s=0;
     }
    return 0;

}
时间: 2024-07-29 21:20:25

poj 2299 求逆序数的相关文章

POJ 2299 求逆序对(归并排序或树状数组)

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

poj 2299 Ultra-QuickSort 归并排序求逆序数对

题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题思路: 根据冒泡排序的特点,我们可知,本题只需要统计每一个数的逆序数(如果有i<j,存在a[i] > a[j],则称a[i]与 a[j]为逆序数对),输出所有的数的逆序数的和用普通排序一定会超时,但是比较快的排序,像快排又无法统计 交换次数,这里就很好地体现了归并排序的优点.典型的利用归并排序求逆

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include

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

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

POJ 2299 Ultra-QuickSort (求逆序数:离散化+树状数组或者归并排序求逆序数)

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

poj 2299 Ultra-QuickSort 求逆序数,树状数组解法,详细解析

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

POJ 2299 Ultra-QuickSort (树状数组+离散化 求逆序数)

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 9 1 0 5

POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

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

poj 1007 DNA Sorting (求逆序数)

DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 83069   Accepted: 33428 Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instanc