poj 2299(离散化+树状数组)

Ultra-QuickSort

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 53777   Accepted: 19766

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

题意:求解一个串的逆序数的个数是多少??

题解:离散化数组变成下标,然后每次将离散化的下标放进树状数组,放进去之后统计小于他的数的个数是多少。用 i - getsum(a[i])即为大于它的数的个数,其中 i 为当前已经插入的数的个数。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 500005;

struct Node{
    int v,id;
}node[N];
int a[N],c[N],n;
int lowbit(int x){
    return x&(-x);
}
void update(int idx,int v){
    for(int i=idx;i<=N;i+=lowbit(i)){
        c[i]+=v;
    }
}
int getsum(int idx){
    int sum = 0;
    for(int i=idx;i>=1;i-=lowbit(i)){
        sum+=c[i];
    }
    return sum;
}
int cmp(Node a,Node b){
    return a.v<b.v;
}
int main()
{
    while(scanf("%d",&n)!=EOF,n){
        memset(c,0,sizeof(c));
        for(int i=1;i<=n;i++){
            scanf("%d",&node[i].v);
            node[i].id = i;
        }
        sort(node+1,node+n+1,cmp);
        for(int i=1;i<=n;i++){
            a[node[i].id] = i;
        }
        long long  cnt = 0;
        for(int i=1;i<=n;i++){
            update(a[i],1);
            cnt=cnt+ i - getsum(a[i]);
        }
        printf("%lld\n",cnt);
    }
    return 0;
}
时间: 2024-08-04 21:15:39

poj 2299(离散化+树状数组)的相关文章

Poj 2299 Ultra-QuickSort 树状数组 解法

本题的树状数组稍微有点特点,就是需要所谓的离散化一下,开始听这个名称好像很神秘的,不过其实很简单. 就是把一个数组arr的值,其中的值是不连续的,变成一组连续的值,因为这样他们的顺序是不变的,所以,不影响结果. 例如:9 1 0 5 4 ->变为:5 2 1 4 3看出他们的相对位置不变的. 9和5为最大值在第一个位置,1和2为第二大的值在第二个位置,0和1在第一个位置等,看出对应顺序了吗? 对,就是这么简单的方法, 就叫做离散化. 如果你对counting sort熟悉的话,那么这样的思想理解

POJ 2299 Ultra-QuickSort(树状数组+离散化)

题目大意: 就是说,给你一个序列,然后让你求出这个序列有多少个逆序对,所谓逆序对就是对于这个序列中的元素有a[i]>a[j] 且i<j存在. 其实原题是这样说的,给你一个序列,让你用最少的交换次数使得这个序列变成从小到大的排序. 解题思路: 一开始是想到了归并的思路,但是没有能写出来代码. 先来来范围吧,序列的长度n<=500000+4.   并且每个a[i]<=999 999 999,对于tree[i],我们知道这个数组肯定是放不下的,所以 我们要进行离散化的处理,关于离散化的处

POJ 2299 Ultra-QuickSort (树状数组+离散化 求逆序数)

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

POJ 2299 Ultra-QuickSort(树状数组 + 离散)

链接:http://poj.org/problem?id=2299 题意:给出N个数组成的数列A(0 <= A[i] <= 999,999,999),求该数列逆序对的数量. 分析:题目所谓的排序过程其实就是一个冒泡排序的过程.在这里,我们需要知道,冒泡排序所需交换的次数等于该序列逆序对的数量(证明略).这是这道题的一个切入点. 树状数组可以很方便地求出数列的前缀和,对于一个数x,我们使树状数组上第x个元素的值赋为1,这时调用Sum(x)就可以得到一个从第1项到第x项的前缀和.这意味着我们可以通

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

题目大意就是说帮你给一些(n个)乱序的数,让你求冒泡排序需要交换数的次数(n<=500000) 显然不能直接模拟冒泡排序,其实交换的次数就是序列的逆序对数. 由于数据范围是 0 ≤ a[i] ≤ 999,999,999所以先要离散化,然后用合适的数据结果求出逆序 可以用线段树一步一步添加a[i],每添加前查询前面添加比它的大的有多少个就可以了. 也可用树状数组,由于树状数组求的是(1...x)的数量和所以每次添加前查询i-sum(a[i])即可 树状数组: //5620K 688MS #incl

poj 2299 Ultra-QuickSort(树状数组)

题意:求一个数列的冒泡排序的交换次数: 参考:http://blog.csdn.net/suwei19870312/article/details/5293694 思路: 一个数列的冒泡排序交换次数即为每个数对应的逆序对数之和,朴素的思想是两个for,O(N^2)复杂度: 数字范围是0-999999999,数组大小为500000,所以先离散化,用结构体记录原数列的下标和值: 对于第i个数,利用树状数组的结构,将数列中的数逐个插入到树状数组中并统计当前树状数组中在该数之前的数的个数num,i-nu

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

Language: Default Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 45751   Accepted: 16615 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct i

poj 2299 Ultra-QuickSort(树状数组)

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 seque

POJ 2299 Ultra-QuickSort (树状数组 &amp;&amp; 离散化&amp;&amp;逆序)

题意 : 给出一个数n(n<500,000), 再给出n个数的序列 a1.a2.....an每一个ai的范围是 0~999,999,999  要求出当通过相邻两项交换的方法进行升序排序时需要交换的次数 分析:其实经过一次模拟后,会发现奇妙的东西,这个排序都是按位置排的,最大要求到最大,最小要去到最小,转化思想这是一道求逆序对数的题目,答案就是逆序对数. 这里数据过大999999999,数组无法开的了这么大,我们可以离散化,只记录相对大小. 这里离散化有所不同,这里为了压时,用了空间换时间的方法.