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. UVC调试

    USB video class(又称为USB video device class or UVC)就是USB device class视频产品在不需要安装任何的驱动程序下即插即用,包括摄像头.数字摄影 ...

  2. 莫比乌斯函数&莫比乌斯反演

    莫比乌斯函数:http://wenku.baidu.com/view/fbec9c63ba1aa8114431d9ac.html Orz  PoPoQQQ

  3. 使用navicat工具创建MySQL存储过程

    使用Navicat for MySQL工具创建存储过程步骤: 1. 新建函数(选择函数标签 -> 点击新建函数): 2.输入函数的参数个数.参数名.参数类型等: 3.编写存储过程:  代码如下: ...

  4. 如何在Windows 7 或Vista中修改MTU

    Windows操作系统使用Maximum Transmission Unit (MTU) 来确定在下面的网络层上可以传输的协议数据包(protocol data packet)的最大尺寸. MTU参数 ...

  5. 开源项目-SlideMenu和actionbarsherlock的配置

    SlidingMenu 是github上一个非常优秀的开源库,利用它可以很方便的实现左右侧滑菜单的效果,现在这个基本上应用的标配了,如果一个App没有滑动效果基本上是不可能的,中国人都是本着人无我有, ...

  6. Redis自学笔记—PHP

    connect 实例连接到一个Redis. $redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); var_dum ...

  7. MongoDB学习笔记(五)--复制集 && sharding分片

    主从复制                                                                                       主从节点开启 主节 ...

  8. Style 的查找 FindResource

    1)根据名称查找 PrintPreview fe = new PrintPreview(new Summary()); string strResourceHeader = "headerS ...

  9. Cognos开发图表乱码问题

    在此之前提到过在利用TR建模导入IQD数据源的时候遇到乱码的一种解决方案: http://www.cnblogs.com/wxjnew/p/3374029.html 今天说的是在RS中开发新报表的时候 ...

  10. (转)Unity3D命令行Build

    转自:http://www.cnblogs.com/gameprogram/archive/2012/05/11/2496303.html 本来是没想用这个命令行Build方式,可惜电脑不知道怎么的就 ...