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
Output
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(归并排序+离散化树状数组)的更多相关文章
- HDU 6318.Swaps and Inversions-求逆序对-线段树 or 归并排序 or 离散化+树状数组 (2018 Multi-University Training Contest 2 1010)
6318.Swaps and Inversions 这个题就是找逆序对,然后逆序对数*min(x,y)就可以了. 官方题解:注意到逆序对=交换相邻需要交换的次数,那么输出 逆序对个数 即可. 求逆序对 ...
- poj-----Ultra-QuickSort(离散化+树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 38258 Accepted: 13784 ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组
BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【bzoj4627】[BeiJing2016]回转寿司 离散化+树状数组
题目描述 给出一个长度为n的序列,求所有元素的和在[L,R]范围内的连续子序列的个数. 输入 第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限. 第二行包含N个整数Ai,表示小Z对寿 ...
- 【bzoj5055】膜法师 离散化+树状数组
题目描述 给定一个序列$a$,求满足$i<j<k$且$a_i<a_j<a_k$的三元组$(i,j,k)$的个数. 输入 第一行1个数 n 第二行n个数 a_i 输出 一个数,表 ...
随机推荐
- !!!易控INSPEC组态软件开发小结——-一次工程文件损坏和处理经过
从加入红橡开始熟悉和使用易控(INSPEC)组态软件,值得赞扬的是INSPEC的开放性和对C#语言的支持,除此之外,便也没有感觉它与其他组态软件有太多优势,有人说INSPEC软件授权比国内其他同类的组 ...
- C++运算符重载为非成员函数
#include<iostream> using namespace std; class Complex{ public: Complex(double r=0.0,double i=0 ...
- UITextView 限制输入字数
尊重原创 http://blog.csdn.net/fengsh998/article/details/45421107 对于限制UITextView输入的字符数.相信大家在网上见得最多的是实现UI ...
- poj1084Square Destroyer(LDX解重复覆盖)
题目请戳这里 题目大意:给一个n*n的用单位长度的木棍拼起来的网格图,给每个木棍按图示编号,编号范围1~2*n*(n+1).现在已知图中已经去掉了k个木棍,求还要至少去掉几根木棍能使网格图中不存在正方 ...
- 神奇的魔法数字0x61c88647
来源JDK源码,产生的数字分布很均匀 用法代码如下. # -*- coding: utf-8 -*- HASH_INCREMENT = 0x61c88647 def magic_hash(n): fo ...
- ubuntu NFS
NFS(Network File System, 网络文件系统)可以通过网络将分享不同主机(不同的OS)的目录——可以通过NFS挂载远程主机的目录, 访问该目录就像访问本地目录一样! 一般而言, 使用 ...
- 两种解决Qt5显示中文乱码的方法(使用QStringLiteral和#pragma execution_character_set("utf-8")两种方法)
升级到Qt5.X之后,原先解决的Qt显示中文乱码的方法突然不适用了,找了很多方式来解决这个问题第一种:在公司代码里看到的方法,先将对应的cpp文件用windows自带的记事本打开,另存为UTF-8格式 ...
- BZOJ 2463 谁能赢呢? (博弈论)
题解:简单博弈论 #include <cstdio> int main(){ int n; while(scanf("%d",&n),n!=0) if (n&a ...
- HDU 1348 Wall
题解:计算凸包周长 #include <iostream> #include <cmath> #include <algorithm> const int size ...
- SSH 配置日记
1 注意struts2-spring-plugin.jar的导入. Unable to load configuration. - action 异常.需要导入这个包 2 很久都跑不通的 ...