Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 52778   Accepted: 19348

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<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
#define MAX 500500
using namespace std;
int n,m;
struct node
{
int val,pos;
}num[MAX];
int sum[MAX<<2];
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void gettree(int o,int l,int r)
{
sum[o]=0;
if(l==r)
return ;
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
} void update(int o,int l,int r,int pos)
{
if(l==r)
{
sum[o]+=1;
return ;
}
int mid=(l+r)>>1;
if(pos<=mid) update(o<<1,l,mid,pos);
else update(o<<1|1,mid+1,r,pos);
pushup(o);
}
int find(int o,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
return sum[o];
int mid=(l+r)>>1;
int ans=0;
if(L<=mid) ans+=find(o<<1,l,mid,L,R);
if(R>mid) ans+=find(o<<1|1,mid+1,r,L,R);
return ans;
}
bool cmp(node a,node b)
{
if(a.val!=b.val) return a.val<b.val;
else return a.pos<b.pos;
}
int main()
{
int j,i,t,k;
while(scanf("%d",&n),n)
{
gettree(1,1,n);
for(i=1;i<=n;i++)
{
scanf("%d",&num[i].val);
num[i].pos=i;
}
sort(num+1,num+1+n,cmp);
LL ant=0;
for(i=1;i<=n;i++)
{
update(1,1,n,num[i].pos);
ant+=i-find(1,1,n,1,num[i].pos);
}
printf("%lld\n",ant);
}
return 0;
}

  树状数组:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define LL long long
#define MAX 500500
using namespace std;
int n,m;
int c[MAX<<1];
struct node
{
int val,pos;
}num[MAX];
bool cmp(node a,node b)
{
if(a.val!=b.val)
return a.val<b.val;
else
return a.pos<b.pos;
}
void update(int x)
{
while(x<=n)
{
c[x]+=1;
x+=x&-x;
}
}
LL sum(int x)
{
LL ans=0;
while(x>=1)
{
ans+=c[x];
x-=x&-x;
}
return ans;
}
int main()
{
int j,i,t,k;
while(scanf("%d",&n),n)
{
for(i=1;i<=n;i++)
{
scanf("%d",&num[i].val);
num[i].pos=i;
}
sort(num+1,num+n+1,cmp);
memset(c,0,sizeof(c));
LL ant=0;
for(i=1;i<=n;i++)
{
update(num[i].pos);
ant+=i-sum(num[i].pos);
}
printf("%lld\n",ant);
}
return 0;
}

  

poj 2299 Ultra-QuickSort(求逆序对)的更多相关文章

  1. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

  2. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  3. POJ 2188线段树求逆序对

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

  4. Poj 2299 Ultra-QuickSort(归并排序求逆序数)

    一.题意 给定数组,求交换几次相邻元素能是数组有序. 二.题解 刚开始以为是水题,心想这不就是简单的冒泡排序么.但是毫无疑问地超时了,因为题目中n<500000,而冒泡排序总的平均时间复杂度为, ...

  5. POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对

    http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...

  6. 树状数组求逆序对:POJ 2299、3067

    前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...

  7. POJ 2299树状数组求逆序对

    求逆序对最常用的方法就是树状数组了,确实,树状数组是非常优秀的一种算法.在做POJ2299时,接触到了这个算法,理解起来还是有一定难度的,那么下面我就总结一下思路: 首先:因为题目中a[i]可以到99 ...

  8. Ultra-QuickSort POJ - 2299 树状数组求逆序对

    In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a seque ...

  9. POJ 2299 求逆序对个数 归并排序 Or数据结构

    题意: 求逆序对个数 没有重复数字 线段树实现: 离散化. 单点修改,区间求和 // by SiriusRen #include <cstdio> #include <cstring ...

随机推荐

  1. [ionic开源项目教程] - 第3讲 左右滑动菜单的实现(使用Tabs和SlideBox)

    使用Tabs和SlideBox实现左右滑动菜单 1.将tab1.html的代码改为如下: <ion-view view-title="健康"> <ion-cont ...

  2. VS2015新功能

    今天有幸参加了微软的 Visual Studio Dev Day,趁还没有忘记今天的学习内容. 先把这些内容记录下来,如果有其他人也参加此次交流活动,请补充完善. VS2015新功能 1,Roslyn ...

  3. HR免费选人的网站乐跳网上线

    园子里有HR吗? 我们的免费选人的网站乐跳网(http://www.letiao.com)上线了, 免费选人在精英人才榜(http://www.letiao.com/talentwall) 有面试,奖 ...

  4. UVa 839 (递归方式读取二叉树) Not so Mobile

    题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然 ...

  5. BZOJ 2429 聪明的猴子

    kruskal. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...

  6. python编码问题(1)

    一.字符编码基础 字符编码是计算机对字符的格式化,从而能够在计算机系统中存储与传输. 1.ASCII码 在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态 ...

  7. 一起学CUDA(一)

    前提是电脑的显卡支持CUDA,N卡一般是支持的,如果是A卡就没办法了.主要针对Windows环境,Linux和Mac也有相应的安装包.CUDA环境搭建:Step1:安装代码环境VS2010:Step2 ...

  8. 【转】不可变数组NSArray与可变数组NSMutableArray

    原文网址:http://www.jianshu.com/p/1ad327f56d1d 不可变数组NSArray //创建一个空数组 NSArray *array = [NSArray array]; ...

  9. ubuntu 查看系统版本信息

    查看cpu信息cat /proc/cpiinfo 查看ubuntu版本:cat /etc/issue 查看系统是32位还是64位方法1:#查看long的位数,返回32或64 getconf LONG_ ...

  10. 左侧菜单 z

    Dev 的tabControl