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(线段树,离散化)的更多相关文章

  1. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  2. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  3. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  4. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  5. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  6. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

  7. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  8. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

  10. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

随机推荐

  1. 自定义类似QMutexLocker的CMutexLocker

    最近做项目遇到一个需求,有一个buttonSlot()执行要耗点时间,为了防止用户无限制的乱点出现问题,考虑加一个互斥锁,使得每次执行完后才允许执行下一次.大概意思是: //QMutex  m_mut ...

  2. Android 播放声音

    public static void PlayAlarmRing(Context mContext) { Uri alert = RingtoneManager.getDefaultUri(Ringt ...

  3. solr 从零学习开始

    2010-10 目 录 1 1.1 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4. ...

  4. MongoDB(二)——安装配置了解

    前边介绍了MongoDB的大概理论知识,这篇来对MongoDB进行一下安装使用,支持安装在windows和linux上,当然了很多其它情况下我们是安装在linux上,由于毕竟server用linux的 ...

  5. IIS - HTTP 错误 500.21 - Internal Server Error 处理程序“WebServiceHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    http://www.cnblogs.com/yc-755909659/p/3445278.html 首先观察,aspnet_regiis.exe文件是不是损坏的,如果是,重新下载,覆盖即可,在百度云 ...

  6. 列举一些常见的Python HTTP服务器

    要使 Python 写的程序能在 Web 上被访问,还需要搭建一个支持 Python 的 HTTP 服务器.下面列举一些常见的 Python HTTP 服务器,以及它们目前的大致发展情况,以便用户的对 ...

  7. EasyUI - SplitButton 分割按钮

    效果: html代码: <!--使用标签创建,直接使用即可,不必使用js代码--> <%--<a href="javascript:void(0)" id= ...

  8. Eclipse用法和技巧三:自动生成Main方法2

    上一篇文章里面介绍了新建文件时候自动添加main方法,这里接着介绍自动联想main方法.       步骤一:输入"main” 步骤二:保持光标在上图位置,按ALT + /,再回车 上一篇文 ...

  9. 升级版:深入浅出Hadoop实战开发(云存储、MapReduce、HBase实战微博、Hive应用、Storm应用)

          Hadoop是一个分布式系统基础架构,由Apache基金会开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力高速运算和存储.Hadoop实现了一个分布式文件系 ...

  10. 【UML九种图系列】之如何利用三层来绘制类图、时序图?

    UML并发视图:实体之间行为的交互,是动态.分为:时序图.协作图.状态图.活动图 一.时序图简述: 时序图(Sequence Diagram):描述对象之间的交互行为,按照时间顺序排列. 元素: 角色 ...