POJ 2299 Ultra-QuickSort 求逆序对数(归并排序,树状数组)

Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 36281 Accepted: 13078

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.

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

Source

 

【题目大意】:

          求对于一数列冒泡的交换次数

【分析】:

         转化题目模型:求序列的逆序对数

      逆序对数:设A[1..n]是一个包含N个非负整数的数组。如果在i<j的情况下,有A[i]>A[j],则(i,j)就称为A中的一个逆序对。例如,数组(3,1,4,5,2)的“逆序对”有<3,1>,<3,2><4,2><5,2>,共4个。定义:对于一个给定的数列,如果有i<j,且Ai>Aj,则称(i,j)为一逆序对. 要解决的问题是,给出一个数列,求出这个数列包含多少个逆序对

            暂时了解到的有三种方法:归并排序,树状数组和权值线段树,代码给出前两种方法

【代码1,归并排序】:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define MAXN 500001
int N,a[MAXN],t[MAXN];
long long ans=0;
void merge(int left,int mid,int right)
{
      int now=0,l=left,r=mid+1;
      while(l<=mid && r<=right)
      {
            if(a[l]>a[r])
            {
                  t[now++]=a[r++];
                  ans+=mid-l+1;
            }
            else  t[now++]=a[l++];
      }
      while(l<=mid)   t[now++]=a[l++];
      while(r<=right) t[now++]=a[r++];
      for(int i=0;i<now;i++)
            a[left+i]=t[i];
}
void mergesort(int left,int right)
{
      if(left<right)
      {
            int middle=(left+right)/2;
            mergesort(left,middle);
            mergesort(middle+1,right);
            merge(left,middle,right);
      }
}
int main()
{
      //freopen("input.in","r",stdin);
	  //freopen("output.out","w",stdout); 
	  while(scanf("%d",&N)!=EOF && N!=0)
	  {
            ans=0;
            for(int i=1;i<=N;i++)
                  scanf("%d",&a[i]);
            mergesort(1,N);
            /*for(int i=1;i<=N;i++)
                  printf("%d ",a[i]);
            printf("\n");*/
            printf("%I64d\n",ans);
      }
	  //system("pause");
      return 0;
}


 

【代码2,树状数组】:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define MAXN 500001
struct DATA{int old,num,v;};
DATA a[MAXN];
int N,kind=0,old[MAXN];
long long S[MAXN],ans=0;
bool cmp(DATA X,DATA Y){return X.old<Y.old;}
bool cmp1(DATA X,DATA Y){return X.num<Y.num;}
int lowbit(int x){return x&(-x);}
void add(int x,int value)
{
      int now=x;
      while(now<=kind)
      {
            S[now]+=value;
            now+=lowbit(now);
      }
}
long long getsum(int x)
{
      int now=x;
      long long tmp=0;
      while(now>0)
      {
            tmp+=S[now];
            now-=lowbit(now);
      }
      return tmp;
}
int main()
{
      //freopen("input.in","r",stdin);
	  //freopen("output.out","w",stdout); 
	  while(scanf("%d",&N)!=EOF && N!=0)
      {
            memset(S,0,sizeof(S));
            ans=0;
            kind=0;
            for(int i=1;i<=N;i++)
            {
                  scanf("%d",&a[i].old);
                  a[i].num=i;
            }
            sort(a+1,a+1+N,cmp);
            int last=-1;
            for(int i=1;i<=N;i++)
            {
                  if(a[i].old!=last)
                  {
                        last=a[i].old;
                        a[i].v=++kind;
                  }
                  else a[i].v=kind;
            }
            sort(a+1,a+1+N,cmp1);
            for(int i=N;i>=1;i--)
            {
                  ans+=getsum(a[i].v);
                  add(a[i].v+1,1);
            }
            printf("%I64d\n",ans);
      }
	  //system("pause");
      return 0;
}


 

转载注明出处: http://blog.csdn.net/u011400953

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ 2182是一道使用树状数组解决的题目,题目要对给定的n个进行排序,并且输出每个在排序后的相对位置。树状数组是一种用来高效处理前缀和问题的据结构。 根据引用中的描述,我们可以通过遍历组a,对于每个元素a[i],可以使用二分查找找到a到a[i-1]中小于a[i]的的个。这个个就是它在排序后的相对位置。 代码中的query函用来前缀和,add函用来更新树状数组。在主函中,我们从后往前遍历组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans组中。 最后,我们按顺序输出ans组的元素即可得到排序后的相对位置。 参考代码如下: ```C++ #include <iostream> #include <cstdio> using namespace std; int n, a += y; } } int main() { scanf("%d", &n); f = 1; for (int i = 2; i <= n; i++) { scanf("%d", &a[i]); f[i = i & -i; } for (int i = n; i >= 1; i--) { int l = 1, r = n; while (l <= r) { int mid = (l + r) / 2; int k = query(mid - 1); if (a[i > k) { l = mid + 1; } else if (a[i < k) { r = mid - 1; } else { while (b[mid]) { mid++; } ans[i = mid; b[mid = true; add(mid, -1); break; } } } for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; } ``` 这段代码使用了树状数组来完成题目要的排序功能,其中query函用来前缀和,add函用来更新树状数组。在主函中,我们从后往前遍历组a,通过二分查找找到每个元素在排序后的相对位置,并将结果存入ans组中。最后,我们按顺序输出ans组的元素即可得到排序后的相对位置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [poj2182Lost Cows——树状数组快速查找](https://blog.csdn.net/aodan5477/article/details/102045839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [poj_2182 线段树/树状数组](https://blog.csdn.net/weixin_34138139/article/details/86389799)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值