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. JS高级-数据结构的封装

    最近在看了<数据结构与算法JavaScript描述>这本书,对大学里学的数据结构做了一次复习(其实差不多忘干净了,哈哈).如果能将这些知识捡起来,融入到实际工作当中,估计编码水平将是一次质 ...

  2. Nginx教程(三) Nginx日志管理

    Nginx教程(三) Nginx日志管理 1 日志管理 1.1 Nginx日志描述 通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某 ...

  3. countDownLatch和cyclicBarrier

    < Effecit In Java >说过,从java 1.5发现版本开始, 就不建议使用wait和notify,它们使用比较困难,可以使用更高级并发工具来替代. 图一所说的同步器是指那些 ...

  4. jQuery插件 -- 图片随页面滚动fixed

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Java之反射--练习

    定义Student 类:包含:姓名和年龄等属性,有参和无参构造方法,输出所有信息的方法 1.使用多种方法生成一个Student类的Class对象 2.使用Class类获取Student类的结构信息并输 ...

  6. python中xrange用法分析

    本文实例讲述了python中xrange用法.分享给大家供大家参考.具体如下: 先来看如下示例: >>> x=xrange(0,8) >>> print x xra ...

  7. 3D旋转动画练习 demo

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. linux shell编程-bash的奇技淫巧

    本文主要讲bash脚本中容易出错和很少用但是用起来有意想不到效果的部分. 循环: 正常的for循环: for i in a b c 1 2 3; do echo "$i" done ...

  9. Chapter 9:Noise-Estimation Algorithms

    作者:桂. 时间:2017-06-14  12:08:57 链接:http://www.cnblogs.com/xingshansi/p/6956556.html 主要是<Speech enha ...

  10. 笔记,spring4+ehcache2配置文件

    最近工作中遇到个功能需要整合ehcache,由于spring版本用的是最新的4.2.4,而在ehcache官网找到的集成配置文档是spring3.1的,因此配了几次都不成功,在历经一番波折后终于成功集 ...