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
题意:有一种排序,规则为如果相邻两数左比右大就交换他们,求最小交换次数?
题解:显然最小次数为逆序对数,至于逆序对,可以归并排序求,也可以树状数组/线段树求,自然是选择简单的喽!
代码如下:
#include<queue>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lson root<<1
#define rson root<<1|1
#define hi puts("hi!");
using namespace std; struct node
{
int kd,val;
}a[]; int n,m,cnt[];
long long tr[]; bool cmp(node a,node b)
{
return a.val<b.val;
} void push_up(int root)
{
tr[root]=tr[lson]+tr[rson];
} void build(int root,int l,int r)
{
if(l==r)
{
tr[root]=;
return;
}
int mid=(l+r)>>;
build(lson,l,mid);
build(rson,mid+,r);
push_up(root);
} void add(int root,int l,int r,int x,int p)
{ if(l==r)
{
tr[root]=;
return;
}
int mid=(l+r)>>;
if(p<=mid)
{
add(lson,l,mid,x,p);
}
if(p>mid)
{
add(rson,mid+,r,x,p);
}
push_up(root);
} long long query(int root,int l,int r,int x,int y)
{
long long ans=;
if(x<=l&&y>=r)
{
return tr[root];
}
int mid=(l+r)>>;
if(x<=mid)
{
ans+=query(lson,l,mid,x,y);
}
if(y>mid)
{
ans+=query(rson,mid+,r,x,y);
}
return ans;
} int main()
{
while(scanf("%d",&n)==&&n)
{
long long ans1=;
memset(tr,,sizeof(tr));
build(,,n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].val);
a[i].kd=i;
}
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)
{
cnt[a[i].kd]=i;
}
for(int i=n;i>=;i--)
{
ans1+=query(,,n,,cnt[i]);
add(,,n,,cnt[i]);
}
printf("%lld\n",ans1);
}
}

 

poj2299 Ultra-QuickSort(线段树求逆序对)的更多相关文章

  1. 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)

    题目链接:http://codevs.cn/problem/4163/ 题目:

  2. BNU 2418 Ultra-QuickSort (线段树求逆序对)

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...

  3. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  4. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  5. hdu1394(线段树求逆序对)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 线段树功能:update:单点增减 query:区间求和 分析:如果是0到n-1的排列,那么如果 ...

  6. HDU 1394 线段树求逆序对

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  7. FZU2018级算法第五次作业 m_sort(归并排序或线段树求逆序对)

    首先对某人在未经冰少允许情况下登录冰少账号原模原样复制其代码并且直接提交的赤裸裸剽窃行为,并且最终被评为优秀作业提出抗议! 题目大意: 给一个数组含n个数(1<=n<=5e5),求使用冒泡 ...

  8. POJ 2188线段树求逆序对

    题目给的输入是大坑,算法倒是很简单-- 输入的是绳子的编号wire ID,而不是上(或下)挂钩对应下(或上)挂钩的编号. 所以要转换编号,转换成挂钩的顺序,然后再求逆序数. 知道了这个以后直接乱搞就可 ...

  9. HDU 1394 Minimum Inversion Number(线段树求逆序对)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1394 解题报告:给出一个序列,求出这个序列的逆序数,然后依次将第一个数移动到最后一位,求在这个过程中 ...

随机推荐

  1. CAN总线远程帧和错误帧

    远程帧 通常,数据传输是由数据源节点(例如,传感器发出数据帧)自主完成的.但也可能存在目标节点向源节点请求发送数据的情况.要做到这一点,目标节点需发送一个远程帧,其中的标识符应与所需数据帧的标识符相匹 ...

  2. nodejs 中的 NODE_PATH

    在使用 nodejs 开发中我们都免不了要去安装一些第三方模块. 那么你或多或少的遇到过以下一些问题 再继续阅读之前,我们先来弄清楚一个概念. npm install --global xxx 属于全 ...

  3. mysql实战优化之二:limit优化(大表翻页查询时) sql优化

    mysql的表test中有20105119行数据.建立索引:data_status,place_cargo_status 场景1: SELECT id, resource_id, resource_t ...

  4. 20181101_将WCF寄宿到控制台

    使用管理员权限打开VS2017 2. 创建以下代码进行测试: a)         创建一个空白解决方案 b)         创建三个类库文件 c)         IMathService代码如下 ...

  5. 如何正确且成功破解跨平台数据库管理工具DbVisualizer?(图文详解)

    前期博客 基于JDBC的跨平台数据库管理工具DbVisualizer安装步骤(图文详解)(博主推荐) 上图,所示,说明这个还只是免费版而已,没又破解为Pro版本. 接下来,就是带领大家如何正确且成功破 ...

  6. Linux机器工作环境安装

    安装gcc编译器: yum -y install gcc 安装wget: yum -y install wget 安装python-setuptools: wget http://peak.telec ...

  7. Git第三方仓库安装方式(IUS)

    1.安装使用里面说的自动化安装脚本 curl https://setup.ius.io | sh 2.然后可以看到 git2u相关内容 yum search git 3.执行安装,并查看下版本 yum ...

  8. Mycat实战之新增基于hash分片的表

    1. 修改rule.xml hash分片规则 主要改两个地方: vi rule.xml 分片数量,这里改为3 对应 三个库 hash规则 默认是id列 这里为 PROVINCE 2. reload 加 ...

  9. libevent网络编程汇总

    libevent源码剖析: ========================================================== 1.libevent源码剖析一(序) 2.libeve ...

  10. Ant工具 ant的安装与配置 ant作用

    原文出自:http://blog.csdn.net/zhuche110/article/details/2663904点击打开链接 Ant是一种基于Java的build工具.理论上来说,它有些类似于( ...