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. Codeforces Round #275 (Div. 2)

    A. Counterexample 题意:给出l,r,找出使得满足l<a<b<c<r,同时满足a,b的最大公约数为1,b,c的最大公约数为1,且a,b的最大公约数不为1 因为题 ...

  2. [Swift 语法点滴]—— Struct Vs Class

    摘自:stackoverflow.com/questions/24232799/why-choose-struct-over-class Structure instances are always ...

  3. 集成框架jar包的一些选择

    我们往往在官网上下载了框架需要的jar却不知道该导入什么. required包里面的是必须导入的jar jar应该一切从简

  4. 【Django】Python虚拟环境工具virtualenv

    教程 第一步:安装virtualenv $pip install virtualenv 第二步:开启虚拟环境的python $cd ENV/Scripts $activate.bat #启用virtu ...

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

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

  6. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:4.安装Oracle RAC FAQ-4.2.Oracleasm Createdisk ASM磁盘失败:Instantiating disk: failed

    1.错误信息:Instantiating disk: failed [root@linuxrac1 /]# /usr/sbin/oracleasm createdisk OCR_VOTE /dev/s ...

  7. Nodepad++ tab改成4个空格

    设置-首选项-选项卡设置-使用空格替换

  8. 嵌入式 hi3518平台检测网线是否插上

    /********************************** (C) COPYRIGHT ******************************* * File Name        ...

  9. Ajax请求内嵌套Ajax请求的方法

    前段时间做项目,需要把全国省市的两个XML文件整合成一个JSON格式的数据,手写的话觉得数据太多了,而且容易出错,于是就想到了用Ajax嵌套的方法来解决,就想平时用Ajax的方法直接嵌套,都会先读出外 ...

  10. RandomAccessFile、FileChannel、MappedByteBuffer读写文件

    s package com.nio; import java.io.Closeable; import java.io.FileNotFoundException; import java.io.IO ...