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. 文件下载Demo

    知识点: //获取用户要下载的资源的名称        string name=context.Request.Params["downloadName"];        //设 ...

  2. LeetCode Coins in a Line

    There are n coins in a line. Two players take turns to take one or two coins from right side until t ...

  3. BZOJ 1189: [HNOI2007]紧急疏散evacuate( BFS + 二分答案 + 匈牙利 )

    我们可以BFS出每个出口到每个人的最短距离, 然后二分答案, 假设当前答案为m, 把一个出口拆成m个表示m个时间, 点u到出口v的距离为d, 那么u->v的[d, m]所有点连边, 然后跑匈牙利 ...

  4. IDEA 15 社区版 Maven项目 启动Tomcat调试

    1.在pom下添加Tomcat插件: <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifac ...

  5. 经典union的使用

    一个用户下广告位  某一天有收入和支出  有支出不一定有收入  有收入不一定有支出  下例为按用户查询 sanhao 下的信息 支出如下: 收入如下: 按天进行查询,例如查询: 得到结果如下: 使用一 ...

  6. uva 10651 - Pebble Solitaire(记忆化搜索)

    题目链接:10651 - Pebble Solitaire 题目大意:给出一个12格的棋盘,‘o'代表摆放棋子,’-‘代表没有棋子, 当满足’-oo'时, 最右边的棋子可以跳到最左边的位子,而中间的棋 ...

  7. Hierarchical Storage structure

    1.hierarchical storage structure      This notion of inserting a smaller, faster storage device (e.g ...

  8. bootstrap基础样式使用

    <small> 为了给段落添加强调文本,则可以添加 class="lead" <small>本行内容是在标签内</small><br> ...

  9. 无法编辑的word解密

    打开文档后,将其另存为XML文件,然后用UltraEdit(或者EditPlus,下载华军里搜索一下就行了)这个编辑软件打开刚刚存储的 XLM文件,查找<w:documentProtection ...

  10. C#中析构函数,命名空间及字符串的运用(Ninth day)

    又到了总结知识的时间了,今天在云和学院学习了析构函数,命名空间及字符串的处理,现在就为大家总结下来. 理论: 析构函数 不能在结构中定义析构函数.只能对类使用析构函数. 一个类只能有一个析构函数. 无 ...