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. IaaS vs PaaS vs SaaS

    在云计算的早期阶段,企业面临的最大问题是他们是否应该使用公共云服务.如今,几乎所有的组织都在采用一些公共云服务.更重要的问题是企业应该使用哪种云服务:基础设施即服务(IaaS),平台即服务(PaaS) ...

  2. Java中判断非空对象.

    Java中经常会遇到判断非空的时候. 有的时候判断了非空但是还是报空指针,为什么.? 判断的时候一般都会判断两次.类似于: Org o = new Org(); if ( o.getId()!=nul ...

  3. 1039 Course List for Student

    题意:给出K门课程(编号1~K)以及报名该课程的学生,然后有N个学生查询,对于每一个查询,输出该学生所报的相关课程编号,且要求编号按增序输出. 思路:题目不难,解析略.(本来用map直接映射,用STL ...

  4. Angular2快速入门-2.创建一个新闻列表

    背景: 我们想通过一个例子,展示下Angular2 怎么绑定页面,怎么创建Component, 例子:我们创建一个新闻列表,当点击新闻列表中某一条新闻的时候,展示出该条新闻的详细信息, 在详细信息中可 ...

  5. mac下自定义伪协议配置

    之前查了很多资料,最近也在挖掘研究这方面的漏洞. windows的很简单,在注册表配置就好了,但是mac os 是unix的,没有注册表这么一说. 但是发现腾讯等配置了自定义等协议,例如:tencen ...

  6. 【面试】D

    昨天去了慕名已久的Dell面试(Dell自2015年退出了世界500强的评比),一面基本合格,二面基本没答上... 对公司的整体印象非常好(每个人桌上都有两台很大的显示器:9:00-15:30,如果能 ...

  7. FB联网无人机取得重大进展 实现首次成功着陆

    科技讯6月30日消息,据Engadget报道,在过去的几年里,世界最大社交网络Facebook始终在测试其太阳能无人飞机Aquila.2016年6月份,这种联网无人机在美国亚利桑那州的尤马进行了首次全 ...

  8. Java微信公众平台开发(一)--接入微信公众平台

    转自:http://www.cuiyongzhi.com/post/38.html (一)接入流程解析 在我们的开发过程中无论如何最好的参考工具当然是我们的官方文档了:http://mp.weixin ...

  9. 用sysbench压测MySQL,通过orzdba监控MySQL

    1.1 安装sysbench wget https://codeload.github.com/akopytov/sysbench/zip/0.5 unzip 0.5 cd sysbench-0.5/ ...

  10. centos7 时间设置及ntp

    转自:https://blog.gtwang.org/linux/howto-set-date-time-from-linux-command-prompt/ 這裡介紹各種在 Linux 中以手動來調 ...