Ultra-QuickSort
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 38258   Accepted: 13784

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

Source

 
代码:
 //离散化+树状数组
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 500000
struct node
{
int val;
int id;
};
node stu[maxn+];
int n;
__int64 cnt;
int bb[maxn+];
int lowbit(int x)
{
return x&(-x);
}
void ope(int x)
{
while(x<=n)
{
bb[x]++;
x+=lowbit(x);
}
}
int sum(int x)
{
int ans=;
while(x>)
{
ans+=bb[x];
x-=lowbit(x);
}
return ans;
}
int cmp(const void *a ,const void *b)
{
return (*(node *)a).val -(*(node *)b).val;
}
int main()
{
int i;
while(scanf("%d",&n),n)
{
memset(bb,,sizeof(int)*(n+));
for(i=;i<n;i++)
{
scanf("%d",&stu[i].val);
stu[i].id=i+;
}
qsort(stu,n,sizeof(stu[]),cmp);
cnt=;
for(i=;i<n;i++)
{
cnt+=sum(n)-sum(stu[i].id);
ope(stu[i].id);
}
printf("%I64d\n",cnt);
}
return ;
}

归并排序:

代码:

 //归并排序求逆序数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 500000
int cc[maxn+];
int aa[maxn+];
__int64 cnt;
void merge(int low ,int mid,int hig)
{
int i,j,k;
i=low;
j=mid;
k=;
while(i<mid&&j<hig)
{
if(aa[i]>aa[j])
{
cc[k++]=aa[j++];
cnt+=mid-i;
}
else
cc[k++]=aa[i++];
}
while(i<mid)
cc[k++]=aa[i++];
while(j<hig)
cc[k++]=aa[j++];
for(k=,i=low;i<hig;i++)
aa[i]=cc[k++];
}
void merge_sort(int st,int en)
{
int mid;
if(st+<en)
{
mid=st+(en-st)/;
merge_sort(st,mid);
merge_sort(mid,en);
merge(st,mid,en);
}
}
int main()
{
int n,i;
while(scanf("%d",&n),n)
{
cnt=;
for(i=;i<n;i++)
scanf("%d",aa+i);
merge_sort(,n);
printf("%I64d\n",cnt);
}
return ;
}

非递归归并排序:
代码:

 //归并排序求逆序数
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define maxn 500000
int cc[maxn+];
int aa[maxn+];
__int64 cnt;
void merge(int low ,int mid,int hig)
{
int i,j,k;
i=low;
j=mid;
k=;
while(i<mid&&j<hig)
{
if(aa[i]>aa[j])
{
cc[k++]=aa[j++];
cnt+=mid-i;
}
else
cc[k++]=aa[i++];
}
while(i<mid)
cc[k++]=aa[i++];
while(j<hig)
cc[k++]=aa[j++];
for(k=,i=low;i<hig;i++)
aa[i]=cc[k++];
}
//void merge_sort(int st,int en)
//{
// int mid;
// if(st+1<en)
// {
// mid=st+(en-st)/2;
// merge_sort(st,mid);
// merge_sort(mid,en);
// merge(st,mid,en);
// }
//}
void merge_sort(int st,int en)
{
int s,t,i;
t=;
while(t<=(en-st))
{
s=t;
t<<=;
i=st;
while(i+t<=en)
{
merge(i,i+s,i+t);
i+=t;
}
if(i+s<en)
merge(i,i+s,en);
}
if(i+s<en)
merge(i,i+s,en);
}
int main()
{
int n,i;
while(scanf("%d",&n),n)
{
cnt=;
for(i=;i<n;i++)
scanf("%d",aa+i);
merge_sort(,n);
printf("%I64d\n",cnt);
}
return ;
}

poj-----Ultra-QuickSort(离散化+树状数组)的更多相关文章

  1. Poj 2299 - Ultra-QuickSort 离散化,树状数组,逆序对

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 ...

  2. CodeForces 540E - Infinite Inversions(离散化+树状数组)

    花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...

  3. Ultra-QuickSort(归并排序+离散化树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50517   Accepted: 18534 ...

  4. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  5. BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组

    BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...

  6. Code Forces 652D Nested Segments(离散化+树状数组)

     Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. 【bzoj4627】[BeiJing2016]回转寿司 离散化+树状数组

    题目描述 给出一个长度为n的序列,求所有元素的和在[L,R]范围内的连续子序列的个数. 输入 第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限. 第二行包含N个整数Ai,表示小Z对寿 ...

  9. HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)

    6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...

随机推荐

  1. Javascript数组操作及索引

    1:清空数组最高效的做法 parentThis.PaperQuestionStrategiesList.length = 0;   2:push and pop parentThis.PaperQue ...

  2. [转]Unicode和UTF-8的关系

    Unicode和UTF-8的关系作者: 张军 原文地址: http://blog.renren.com/blog/284133452/485453790 今天中午,我突然想搞清楚Unicode和UTF ...

  3. 使用自由软件Icarus Verilog Simulator进行仿真

    Icarus Verilog Simulator(http://iverilog.icarus.com/home)使用iverilog作为源代码编译器,编译生成vvp程序文本,使用vvp作为运行时引擎 ...

  4. Linux 挂载和卸载U盘

    一般的U盘挂载方法: mount [-fnrsvw]  [-t vfstype] [-o options] device dir 参数:device表示要挂载的设备,dir表示挂载点 -t 指定设备的 ...

  5. iOS开发-UITableView常用方法

    UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewData ...

  6. IDEA是如何导入项目的,及启动导入项目遇到的问题:无法加载主类的一连串问题

    1.启动报错误: 找不到或无法加载主类 org.spring.springboot.Application 可能在工程下面有多个module,然后,module里面的iml配置文件不止一个,删除留主的 ...

  7. Visual Studio环境变量使用实例:使用环境变量来组织project

    前言 在前一篇文章Visual Studio中的环境变量(以Visual Studio 2013为例)中介绍了VS中的环境变量,本文将以实际样例说明怎样合理使用这些环境变量来组织VC++project ...

  8. [Javascript] Funciton Expression

    //This will load the code into the memory no matter //you call it or not function diffOfSquares(a,b) ...

  9. 在Ubuntu 桌面版 12.04 LTS配置sftp

    第一步:检查vsftpd是否安装了 root@hy-Aspire-SA10:~# rpm -qa|grep vsftpd 没有输出,说明没安装. 第二步:安装vsftpd root@hy-Aspire ...

  10. PS常用技能综合

    1.ps中怎么吧选中的一块区域覆盖到另一个区域中? 1.用PS中选取工具或套索工具--选好后用移动工具把你选取的区域移动到你要覆盖的地方2.或用图章工具中的仿制图章 2.如何将选中的区域新建为一个新图 ...