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.

本题的大意是求逆序对吧,只是有多组数据。大伙儿肯定习惯了归并排序;不过,我今天就是要强调的是树状数组。



---------------------------------------------我是分割线-----------------------------------------------



树状树的推导:

假设数组a[1..n],那么查询a[1]+...+a[n]的时间是log级别的,而且是一个在线的数据结构,支持随时修改某个元素的值,复杂度也为log级别。
来观察这个图:

树状数组的结构图

令这棵树的结点编号为C1,C2...Cn。令每个结点的值为这棵树的值的总和,那么容易发现:
C1 = A1
C2 = A1 + A2
C3 = A3
C4 = A1 + A2 + A3 + A4
C5 = A5
C6 = A5 + A6
C7 = A7
C8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8
...
C16 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + A8 + A9 + A10 + A11 + A12 + A13 + A14 + A15 + A16
这里有一个有趣的性质:
设节点编号为x,那么这个节点管辖的区间为2^k(其中k为x二进制末尾0的个数)个元素。因为这个区间最后一个元素必然为Ax,
所以很明显:Cn = A(n – 2^k + 1) + ... + An

预备代码:

lowbit:快速求与该数相关的之前的数.

add(或change):把x位置的值加上d(当然,其实为改变之后的值)

sum:求1-x的和。

(详细代码见下)



---------------------------------------------我是分割线-----------------------------------------------



思路:数据小的时候,我们开一个数组cnt,其范围是一组数中的最大值。cnt[i]表示数字i出现的次数(一开始清零)

我们从头操作,设每次取出的数为x,则它产生的逆序对(以它为较小者的逆序对)就是所有在此之前的比他大的数的个数。

究竟怎么表示呢? 借鉴于前缀和,我们可以用sum(tot)(tot是最大值)-sum(x-1),这就是目前记录的比它大的数(既然已经记录过,它们一定在x的前面。然而,数据范围有很多9,我们得另想办法。观察n<=50万,我们可以把n个数映射到最多为n的数组里再进行处理。别忘了去重。



代码:

#include<stdio.h>
#include<algorithm>
using namespace std;
struct arr{long x,y;}a[500001];
long cnt[500001],tot,n,i;
long long ans;
long lowbit(long x) {return x&(-x);}
void add(long x,long d)
{
  while (x<=n){cnt[x]+=d;x+=lowbit(x);}
}
long sum(long x)
{
  long ans=0;
  while (x>0){ans+=cnt[x];x-=lowbit(x);}
  return ans;
}
bool cmp(arr a,arr b){return (a.x<b.x);}
int main()
{
  scanf("%ld",&n);
  while (n>0)
  {
    ans=0;
    for (i=1;i<=n;i++)
      {
        scanf("%ld",&a[i].x);
      if (a[i].x==a[i-1].x) a[i].y=a[i-1].y;else a[i].y=a[i-1].y+1;//a[i].x记录数据,a[i].y作为映射,在排序里排进去。
      }
    tot=a[n].y;                                                                     //tot为数组个数,开始为最大值。
    sort(a+1,a+n+1,cmp);
    for (i=1;i<=tot;i++) cnt[i]=0;                                                //清零
    for (i=1;i<=n;i++)
    {
      ans+=sum(tot)-sum(a[i].y-1);                                            //加上当前数产生的逆序对。
      add(a[i].y,1);                                                                    //修改cnt
    }
    printf("%lld\n",ans);
    scanf("%ld",&n);
  }
  return 0;
}

poj 2299 Ultra-QuickSort 题解的更多相关文章

  1. POJ 2299 Ultra-QuickSort 简单题解

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 68874   Accepted: 25813 ...

  2. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  3. 逆序数 POJ 2299 Ultra-QuickSort

    题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...

  4. 树状数组求逆序对:POJ 2299、3067

    前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...

  5. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  6. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  7. POJ 2299 Ultra-QuickSort(线段树+离散化)

    题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...

  8. 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  9. poj 2299 逆序数

    http://poj.org/problem?id=2299 坑:答案是long long 输出……!!!!! 题意是:求一个数组进行冒泡排序交换的次数 题解:求逆序数 题解Ⅰ: 归并排序求逆序数 归 ...

随机推荐

  1. TensorFlowSharp入门使用C#编写TensorFlow人工智能应用

    TensorFlowSharp入门使用C#编写TensorFlow人工智能应用学习. TensorFlow简单介绍 TensorFlow 是谷歌的第二代机器学习系统,按照谷歌所说,在某些基准测试中,T ...

  2. Adobe Fireworks CS6 Mac破解版

    Mac下一款快速建站的软件--Adobe Fireworks CS6,小子这里有时间就分享出来给更多需要的朋友. Adobe Fireworks CS6能让您在弹指间创作精美的网站和移动应用程序设计, ...

  3. (转ORCLE导入导出命令)

    oracle数据库导入导出命令! Oracle数据导入导出imp/exp 功能:Oracle数据导入导出imp/exp就相当与oracle数据还原与备份.   大多情况都可以用Oracle数据导入导出 ...

  4. js图片大小限制,设置

    //图片大小自动脚本 function AutoResizeImage(maxWidth, maxHeight, objImg) { var img = new Image(); img.src = ...

  5. 走进javascript——数组的那些事

    Array构造器 如果参数只有一个并且是Number类型,那么就是指定数组的长度,但不能是NaN,如果是多个会被当做参数列表. new Array(12) // (12) [undefined × 1 ...

  6. ftp服务器可以连接但不能传输数据(proftpd)

    问题:在客户端连接FTP服务器(proftpd)时可以正常连接,但是无法正常传输数据 ftp> ls530 Please login with USER and PASSPassive mode ...

  7. WPF 简易的跑马灯效果

    最近项目上要用到跑马灯的效果,和网上不太相同的是,网上大部分都是连续的,而我们要求的是不连续的. 也就是是,界面上就展示4项(展示项数可变),如果有7项要展示的话,则不断的在4个空格里左跳,当然,衔接 ...

  8. PHP cURL的详细使用手册

    PHP cURL的详细使用手册 PHP cURL可以帮助我们简单有效地去抓取网页内容,帮助我们方便的实现抓取功能.本文主要介绍了PHP cURL的使用方法. AD:2013云计算架构师峰会课程资料下载 ...

  9. [HDU1000] A + B Problem

    Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...

  10. CentOS 虚拟机安装详解

    第一步:安装 VMware 官方网站:www.vmware.com 下载百度云链接:http://pan.baidu.com/s/1bphDOWv 密码:0zix VMware 是一个虚拟 PC 的软 ...