标签:des style class blog code http
| 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 swapping two adjacent sequence elements until the sequence is
sorted in ascending order. For the input sequence Input
Output
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
Source
#include <iostream>
using namespace std;
long long a[500010],t[500010],cnt=0;
void gbsort(long long *a,long long l,long long r)
{
if(l<r)
{
long long mid=(l+r)/2;
gbsort(a,l,mid);
gbsort(a,mid+1,r);
long long s=l,e=mid+1;
long p=0;
while(s<=mid&&e<=r)
{
if(a[s]<=a[e])
{
t[p++]=a[s++];
}
else
{
t[p++]=a[e++];
cnt+=mid-s+1;
}
}
while(s<=mid)
{
t[p++]=a[s++];
}
while(e<=l)
{
t[p++]=a[e++];
}
for(int i=0;i<p;i++)
a[l+i]=t[i];
}
}
int main()
{
int n;
while(cin>>n)
{
if(!n)break;
cnt=0;
for(int i=0; i<n; i++)
cin>>a[i];
gbsort(a,0,n-1);
cout<<cnt<<endl;
}
return 0;
}
POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数),布布扣,bubuko.com
POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)
标签:des style class blog code http
原文地址:http://blog.csdn.net/juncoder/article/details/32704725