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. cas4.0 session中返回更多的用户信息

    实现思路: 新增AccoutAttributeDao类继承StubPersonAttributeDao,重写getPerson方法.实际应用中我们只需要修改getPersion方法中的内容,根据实际情 ...

  2. Longest Substring Without Repeating Characters leetcode java

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  3. angular之interceptors拦截器

    <!DOCTYPE html> <html ng-app="nickApp"> <head> <meta charset="UT ...

  4. Oracle卸载后手工删除内容

    使用deinstall卸载oracle后,手工删除Oracle数据库,方法如下: 第一步:停用全部oracle服务 第二步:删除oracle注册表运行regedit在如下路径中找到oracle相关键值 ...

  5. Run Repository Creation Utility (RCU) for Oracle Identity Management components

    Run Repository Creation Utility (RCU) for Oracle Identity Management components         Installing O ...

  6. [Backbone]3. More detail on View

    Change the AppointmentView to have a top-level li tag (instead of the default div tag). var Appointm ...

  7. 【C#】SQL数据库助手类1.0(自用)

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  8. HTML5游戏,五子棋

    在线演示 本地下载 最近html5的游戏还真是不少,这种在线游戏既简单又有趣.收藏几个在午休时间娱乐一下.何乐而不为呢?喜欢研究的可以下载代码看看.超级推荐!

  9. MAC系统XAMPP 中 MySQL命令行client配置使用

    在PHP的学习过程中.MySQL预计是必定会接触的. MySQL的管理相信大家也会使用phpmyadmin: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv ...

  10. HDU1212 Big Number 【同余定理】

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...