poj2299--B - Ultra-QuickSort(线段树,离散化)

Ultra-QuickSort

Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 41215   Accepted: 14915

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

题意:求出最小交换次数,使得数组变得有序(由小到大)。  也就是求出逆序数,这个题归并排序,树状数组,线段树又可以解,可以当做练手用

使用线段树解的话,因为给出的数值很大,所以需要离散化,一开始直接使用map果断的超时了,只能想一个更简便的离散的方法,使用结构体存下一开始的值k和它初始的序号(id1),sort对k进行排序,得到新的序号(id2),通过id1直接改变给出的数组,变为id2,这样只用n + logn的时间就可以离散完成,(注意要判断重复的值,重复的值共享一个id2)。至于线段树部分就是一个模板

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL __int64
#define maxn 600000
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now LL l,LL r,LL rt
struct node{
    LL id1 , id2 ;
    LL k ;
}p[maxn] ;
LL cl[maxn<<2] , a[maxn] ;
bool cmp(node a,node b)
{
    return a.k < b.k ;
}
void push_up(int_now)
{
    cl[rt] = cl[rt<<1] + cl[rt<<1|1] ;
}
void update(LL i,int_now)
{
    if( i < l || i > r )
        return ;
    if( i == l && i==r )
    {
        cl[rt]++ ;
        return ;
    }
    update(i,lson);
    update(i,rson);
    push_up(now);
    return ;
}
LL query(int ll,int rr,int_now)
{
    if( ll > r || rr < l )
        return 0;
    if( ll <= l && r <= rr )
        return cl[rt] ;
    return query(ll,rr,lson) + query(ll,rr,rson);
}
int main()
{
    LL i , n , m , l , r , x , num ;
    while(scanf("%I64d", &m) && m)
    {
        for(i = 0 ; i < m ; i++)
        {
            scanf("%I64d", &a[i]);
            p[i].k = a[i] ;
            p[i].id1 = i ;
        }
        sort(p,p+m,cmp);
        int temp = -1 ;
        n = 0 ;
        for(i = 0 ; i < m ; i++)
        {
            if( p[i].k == temp )
                p[i].id2 = n ;
            else
            {
                p[i].id2 = ++n ;
                temp = p[i].k ;
            }
        }
        for(i = 0 ; i < m ; i++)
            a[ p[i].id1 ] = p[i].id2 ;
        memset(cl,0,sizeof(cl));
        num = 0 ;
        for(i = 0 ; i < m ; i++)
        {
            num += (i - query(1,a[i],root));
            update(a[i],root);
        }
        printf("%I64d\n", num);
    }
    return 0;
}

poj2299--B - Ultra-QuickSort(线段树,离散化),布布扣,bubuko.com

时间: 2024-08-06 02:39:05

poj2299--B - Ultra-QuickSort(线段树,离散化)的相关文章

POJ_2528 Mayor&#39;s poster(线段树+离散化)

题目请点我 题解: 这道题与之前的题目相比重点在于一个映射的预处理,题目所给的区间达到10000000,而最多只有10000个点,如果直接建树的话太过于空旷.把这些区间的左右节点一一对应,最多有4×10000个点,远小于之前的10000000,而且区间之间的对应关系也不会改变. 举个例子: 区间:[2,6],[4,8],[6,10] 我们进行下面对应: 2 4 6 8 10 1 2 3 4 5 则原区间变为[1,3],[2,4],[3,5].可以发现它们之间的覆盖关系并没有改变,但是却紧凑了很多

POJ - 2528 - Mayor&#39;s posters 【线段树+离散化+补点】

http://poj.org/problem?id=2528 #include <cstdio> #include <iostream> #include <set> #include <cstring> #include <string> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAXN = 32768 + 5; in

HDU5124:lines(线段树+离散化)或(离散化思想)

http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input The first line conta

hdu1828 Picture(线段树+离散化+扫描线)两种方法

C - Picture Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or

Poj 2528 Mayor&#39;s posters (线段树+离散化)

题目连接: http://poj.org/problem?id=2528 题目大意: 有10000000块瓷砖,n张海报需要贴在墙上,每张海报所占的宽度和瓷砖宽度一样,长度是瓷砖长度的整数倍,问按照所给海报顺序向瓷砖上贴海报,最后有几张海报是可见的? 解题思路: 因为瓷砖块数和海报张数多,首选线段树,如果按照常规的建树方式,把瓷砖当做数的节点,肯定会MTL......... 所以我们可以用海报的起点和终点当做树的节点,这样树的节点才有20000个,但是这样建树的话,求海报覆盖了那些节点会很复杂,

hdu 5124 lines (线段树+离散化)

lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 620    Accepted Submission(s): 288 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which

poj2528 线段树+离散化

1 //Accepted 1960K 110MS 2 //线段树+离散化 3 //把所有的坐标排序,从小到大编号,建立线段树 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 /** 1

POJ 2528 (线段树+离散化) Mayor&#39;s posters

因为将每个单位都作为一个最小单元的话会爆内存的 所以,将海报的每个端点进行排序,将这些端点最为最小的区间. 毕竟是刚刚接触线段树,理解起来还有些吃力,还是那句话,题做多了慢慢就好了. 萌萌的AC代码君贴上. 1 //#define LOCAL 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int n; 8 struct CPost 9

poj2528--Mayor&#39;s posters(线段树+离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41785   Accepted: 12164 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

【线段树+离散化】POJ2528 Mayor&#39;s posters

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 64939   Accepted: 18770 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post