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
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Ultra-QuickSort produces the output
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
题意:
给一个n 然后再给n个数字 然后把这n个数字 根据冒泡排序来把他整理成从小到大的序列 计算一共交换了几次
这题很久以前做过一次, 不过是用归并排序做的 不过可能是数据有点小 可以直接过
因为最近在学树状数组 所以用树状数组在写了一次
这题用到了离散化 在百度查资料看了好久 能懂是什么意思 就是将数组的数 映射到1-n 省去了很多空间
因为题目给的范围是0-999,999,999 但是n的范围只有5e5 所以可以大大减少数组的空间
毕竟.. 999,999,999的数组 开不了啊
树状数组直接套的模板, 更新的时候 加入一个是加一
归并排序版:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
using namespace std;
int a[500010],temp[500010];
long long ans;
void merge_sort(int *num,int x,int y,int *temp)
{
if (y-x>1)
{
int m=x+(y-x)/2;
int p=x,q=m,i=x;
merge_sort(num,x,m,temp);
merge_sort(num,m,y,temp);
while (p<m||q<y)
{
if (q>=y||(p<m&&num[p]<=num[q]))
temp[i++]=num[p++];
else
{
temp[i++]=num[q++];
ans+=(m-p);
}
}
for (i=x;i<y;i++)
{
num[i]=temp[i];
}
}
}
int main()
{
int n;
while (scanf("%d",&n)!=EOF&&n)
{
for (int i=0;i<n;i++)
scanf("%d",&a[i]);
ans=0;
merge_sort(a,0,n,temp);
printf("%lld\n",ans);
}
return 0;
}
树状数组版:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
const int N=5e5+10;
int reflect[N],bit[N];
int n;
struct node
{
int id,val;
}num[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int val)
{
while (x<=N)
{
bit[x]+=val;
x+=lowbit(x);
}
}
int query(int x)
{
int sum=0;
while(x>0)
{
sum+=bit[x];
x-=lowbit(x);
}
return sum;
}
bool cmp(const node &a,const node &b)
{
return a.val<b.val;
}
int main()
{
while (~scanf("%d",&n) && n>0)
{
memset(bit,0,sizeof(bit));
for (int i=1; i<=n ; i++)
{
scanf("%d",&num[i].val);
num[i].id=i;//直接把数值映射到1-N
}
sort(num+1,num+n+1,cmp);//从小到大排序
long long ans=0;
for (int i=1 ; i<=n ; i++)
{
update(num[i].id,1);
ans+=(i-query(num[i].id));
}
//i是指当前排的这个数应该是第i位的
//如果当前排的这个数的前面并没有i个 就代表出现了逆序数
//i-query(reflect[i])的总和 就是逆序数 也就是交换次数
printf("%lld\n",ans);
}
return 0;
}