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 输出 一个数,表 ...
随机推荐
- MFC下DLL编程(图解)
DLL(Dynamic Link Library,动态链接库)是微软公司为Windows和OS/2操作系统设计一种供应用程序在运行时调用的共享函数库.DLL是应用程序的一种扩展,也是软件共享和重用的传 ...
- Oracle的大数据类型,BIG DATA TYPE
1.CLOB 字符LOB类型,主要用于存储大型英文字符 2.NCLOB 国际语言字符LOB类型,主要用于存储大型非英文字符 3.BLOB 二进制LOB类型,主要用于存储二进制数据 4.BFILE 二进 ...
- C winpcap 网络抓包 并获取IP TCP 协议的相关信息
以太网协议分析函数: void ethernet_protocol_packet_handle (u_char *argument, const struct pcap_pkthdr *packet_ ...
- Linux下使用ps命令来查看Oracle相关的进程
Linux下可以使用ps命令来查看Oracle相关的进程 Oracle Listener 这个命令会列出Oracle Net Listener的进程 [oracle@ www.linuxidc.com ...
- HDU 2227 Find the nondecreasing subsequences
题目大意:给定一个序列,求出其所有的上升子序列. 题解:一开始我以为是动态规划,后来发现离散后树状数组很好做,首先,c保存的是第i位上升子系列有几个,那么树状数组的sum就直接是现在的答案了,不过更新 ...
- Java ThreadLocal 学习
同步机制是为了同步多个线程对相同资源的并发访问,是为了多个线程之间进行通信的有效方式. 而ThreadLocal是隔离多个线程的数据共享,从根本上就不在多个线程之间共享资源(变量),这样当然不需要对多 ...
- CF 191 div2
A.数据量很小,直接爆搞. #include <iostream> #include <cstdio> #include <algorithm> #include ...
- 正则RegEXp
JavaScript RegExp 对象 RegExp 对象 RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具. 直接量语法 /pattern/attributes 创建 RegE ...
- 【转】利用Ajax.BeginForm提交文件
Ajax.BeginForm @using (Ajax.BeginForm("YourAction", "YourController", new AjaxOp ...
- View从Action中获得数据和html helper function(转载)
@model MvcApplication1.Models.M_Person @using MvcApplication1.Models; @{ ViewBag.Title = "GetDa ...