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

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
题解:归并排序注意对dt主数组的更改,由于数据太大999999999所以要离散化
归并:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=500010;
int dt[MAXN],b[MAXN];
LL ans;
void mergesort(int l,int mid,int r){
int ll=l,rr=mid+1,pos=l;
while(ll<=mid&&rr<=r){
if(dt[ll]<=dt[rr])b[pos++]=dt[ll++];
else{
ans+=rr-pos;
b[pos++]=dt[rr++];
}
}
for(int i=ll;i<=mid;i++)b[pos++]=dt[i];
for(int i=rr;i<=r;i++)b[pos++]=dt[i];
for(int i=l;i<=r;i++)dt[i]=b[i];
}
void ms(int l,int r){
if(l<r){
int mid=(l+r)>>1;
ms(l,mid);
ms(mid+1,r);
mergesort(l,mid,r);
}
}
int main(){
int N;
while(~scanf("%d",&N),N){
int i,j;
ans=0;
F(i,N)
SI(dt[i]);
ms(1,N);
PL(ans);puts("");
}
return 0;
}

  离散化树状数组跟归并原理相似;这个是用二分+离散化树状数组写的;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=0;i<x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=500010;
int a[MAXN],b[MAXN],tree[MAXN+1];
LL ans;
int lowbit(int x){return x&(-x);}
void add(int x){
while(x<=MAXN){
tree[x]++;
x+=lowbit(x);
}
}
int sum(int x){
int sm=0;
while(x>0){
sm+=tree[x];
x-=lowbit(x);
}
return sm;
}
int main(){
int N;
while(~scanf("%d",&N),N){
int i,j;
mem(tree,0);
F(i,N)SI(a[i]),b[i]=a[i];
sort(b,b+N);
ans=0;
F(i,N){
int pos=lower_bound(b,b+N,a[i])-b;
ans+=i-sum(pos);
add(pos+1);
}
PL(ans);puts("");
}
return 0;
}

  其实用不到二分,结构体就妥了:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=0;i<x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=500010;
int tree[MAXN+1];
LL ans;
int lowbit(int x){return x&(-x);}
struct Node{
int v,p;
friend bool operator < (Node a,Node b){
return a.v<b.v;
}
}a[MAXN];
void add(int x){
while(x<=MAXN){
tree[x]++;
x+=lowbit(x);
}
}
int sum(int x){
int sm=0;
while(x>0){
sm+=tree[x];
x-=lowbit(x);
}
return sm;
}
int main(){
int N;
while(~scanf("%d",&N),N){
int i,j;
mem(tree,0);
F(i,N)SI(a[i].v),a[i].p=i;
sort(a,a+N);
ans=0;
F(i,N){
ans+=i-sum(a[i].p);
add(a[i].p+1);
}
PL(ans);puts("");
}
return 0;
}

  

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

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

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

  2. poj-----Ultra-QuickSort(离散化+树状数组)

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

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

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

  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. 【bzoj5055】膜法师 离散化+树状数组

    题目描述 给定一个序列$a$,求满足$i<j<k$且$a_i<a_j<a_k$的三元组$(i,j,k)$的个数. 输入 第一行1个数 n 第二行n个数 a_i 输出 一个数,表 ...

随机推荐

  1. Insert into a Cyclic Sorted List

    Given a node from a cyclic linked list which has been sorted, write a function to insert a value int ...

  2. Immediate Decodability问题Java解答

    DescriptionAn encoding of a set of symbols is said to be immediately decodable if no code for one sy ...

  3. BZOJ 1005: [HNOI2008]明明的烦恼( 组合数学 + 高精度 )

    首先要知道一种prufer数列的东西...一个prufer数列和一颗树对应..然后树上一个点的度数-1是这个点在prufer数列中出现次数..这样就转成一个排列组合的问题了.算个可重集的排列数和组合数 ...

  4. servlet操作数据库

    工具:myeclipse 数据库工具:mysql java ee操作数据库,首先要导入数据库驱动文件,我用的是mysql 刚开始,很多人代码正确但是就是连接不上,原因就是忘了驱动文件的导入. 我的驱动 ...

  5. HDU2084-数塔

    描述: 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 代码: 简单的动态规划 ...

  6. Android 开发笔记 “java.util.Calendar.compareTo()”

     java.util.Calendar.compareTo() 方法比较Calendar对象和anotherCalendar对象之间的时间值(毫秒偏移量). 声明 以下是java.util.Calen ...

  7. python的内置函数bin()

    bin(x) 中文说明:将整数x转换为二进制字符串,如果x不为Python中int类型,x必须包含方法__index__()并且返回值为integer: 参数x:整数或者包含__index__()方法 ...

  8. java从c struct传来的字节数组中取值

    public int getInt(byte[] array,int index) { return (array[index]  & 0xff)  | (array[index + 1] & ...

  9. ORA-20000:ORU-10027:buffer overflow,limit of 10000 bytes错误4

    今天再测试一个存储过程时,用DBMS_OUTPUT.PUT_LINE输出时,报 ORA-20000:ORU-10027:buffer overflow,limit of 10000 bytes SQL ...

  10. 贫血模型or领域模型

    参考: http://lifethinker.iteye.com/blog/283668 http://www.uml.org.cn/mxdx/200907132.asp http://www.itu ...