POJ 2299 Ultra-QuickSort(树状数组求逆序对)

原题描述:
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 62187 Accepted: 23121
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
题目大意:
给定你多个序列,问其中的逆序对个数(数据极大!!!)


首先各位同学看到逆序对第一眼想到的应该就是归并排序了吧。
但如果这样编完,会发现自己会T很多。
这里就给大家介绍一种快速求逆序对个数的方法:离散化+树状数组!


1.离散化:
对于一个无序序列,应该很自然就会想到将其排成有序序列离散化,而本题的做法也是这样的。
举例来说,
原本序列:1 7 2 4 3 6
对应位置:1 2 3 4 5 6
离散化以后(以位值为离散化标准)
当前序列:1 2 3 4 5 6
对应位置:1 3 5 4 6 2
代码实现因该也不难:

struct node{int pos,date;}a[500001];
 for(i=1;i<=n;i++)
 {
   a[i].pos=i;
   a[i].date=read();
 }

那么此时我们就将求值的逆序对转化为了求位置的逆序对了。
然后我们再引入一个b数组,其下标表示真实位置,其值表示在刚刚离散化的序列中所处位置。
具体来说,
for(i=1;i<=n;i++)b[a[i].pos]=i;
举例来说,还是刚刚那个例子中的序列,第一遍按位值离散化,再赋给b后,为:
a[1].pos=1;————>b[1]=1;
a[2].pos=3;————>b[3]=2;
a[3].pos=5;————>b[5]=3;
a[4].pos=4;————>b[4]=4;
a[5].pos=6;————>b[6]=5;
a[6].pos=2;————>b[2]=6;
然后将b排序:b[1..6]={1 6 2 4 3 5};
其实b[i]=j表示的就是第i个位置上的为第j大的数。
做到这里,离散化就基本做完了,由于我们的每一步离散化都基于值的大小,所以此时我们可以用b推出某个数前方有多少个小于其的数。(再看下文前读者不妨先独立思考一下)。


2.树状数组:
在我们离散化得到b数组后,我们就要开始利用树状数组求逆序对的个数了。
准确的说,我们利用b所求的为“顺序对的个数”。
而逆序对的个数=总序对个数-顺序对个数。
由于b的下标表示的是当前位置值的大小排名,
所以我们从1—n依次向树状数组中插入b,一定保证了值的大小顺序。
我们记每个插入节点的贡献为1,
那么其实每插入一个b,他的顺序对个数就为它前面所有节点贡献之和。
为什么呢,其实也并不是很难理解。
依旧以上面的例子说明(顺便回顾一下前面的步骤):
原本序列:1 7 2 4 3 6
对应位置:1 2 3 4 5 6
离散化以后(以位值为离散化标准)
当前序列:1 2 3 4 5 6
对应位置:1 3 5 4 6 2
a[1].pos=1;————>b[1]=1;
a[2].pos=3;————>b[3]=2;
a[3].pos=5;————>b[5]=3;
a[4].pos=4;————>b[4]=4;
a[5].pos=6;————>b[6]=5;
a[6].pos=2;————>b[2]=6;
—>b[1]=1,b[2]=6,b[3]=2,b[4]=4,b[5]=3,b[6]=5;
我们用一条线段表示当前插入情况(求线段区间和读者们自己用树状数组实现,这里只解释说明原理):
我们用“—”表示还未插入。
初始状态:— — — — — —

插入b[1]后:
线段状态:1 — — — — —
对应位值:1
此时和为1,构成的顺对个数为1,逆序对个数为i-1=0;

插入b[2]后:
线段状态:1 — — — — 1
对应位值:1 — — — — 2
当前的和为1+1=2,即构成的顺序对个数为2个,逆序对个数为i-2=0;

插入b[3]=2后:
线段状态:1 1 — — — 1
对应b值: 1 3 — — — 2
我们重点分析这个步骤:
对于当前位置,和为2,构成了两个顺序对,而此时插入的为第三个数,本来前方和应该为3,故组成逆序对个数为3-2=1;回过头看,第三个位置上的2组成逆序对是1个。
后面的依次类推。。。。。

为什么会这样,其实稍微想一想就知道了,我们插入的b[i]表示第i个位置上的数,为第b[i]大的数。如果前方全部为顺序对,那么前方和应该为i,而如果少了,就说明本应该比它小的数跑到后面去了,就构成了逆序对。
上面这一部分较难理解,希望读者们多加体会。


最后给出完整的代码:

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

struct node{int pos,date;}a[500001];
int b[500001],t[2000001];
long long ans;
int n;

int gi() {
    int w;bool q=1;char c;
    while (((c=getchar())<'0'||'9'<c)&&c!='-');
    if (c=='-') q=0,c=getchar();
    w=c-'0';
    while ('0'<=(c=getchar())&&c<='9') w=w*10+c-'0';
    return q?w:-w;
}

inline bool cmp(node a,node b){
  return a.date<b.date;
}

int lowbit(int a){return a&(-a);
}

void insert(int x,int val)
{
    while(x<=n)
    {
      t[x]+=val;
      x=x+lowbit(x);
    }return;
}

int getsum(int x)
{
  int sum=0;
  while(x>0)
    {
      sum+=t[x];
      x=x-lowbit(x);
    }
  return sum;
}

int main()
{
     freopen("A.txt","r",stdin);
     int i,j;
    while(1)
    {
      n=gi();
      if(n==0)break;
      for(i=1;i<=n;i++)
      {
      a[i].pos=i;
      a[i].date=gi();
      }
      sort(a+1,a+n+1,cmp);
      for(i=1;i<=n;i++)b[a[i].pos]=i;
      ans=0;
      memset(t,0,sizeof(t));
      for(i=1;i<=n;i++)
      {
        insert(b[i],1);
        ans+=i-getsum(b[i]);
      }
      printf("%lld\n",ans);
    }
    return 0;
}

Hahahaha!

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、付费专栏及课程。

余额充值