POJ - 2299 Ultra-QuickSort(归并排序)
https://vjudge.net/problem/POJ-2299
题意
求对于给定的无序数组,求出经过最少多少次相邻元素的交换之后,可以使数组从小到大有序。
分析
很明显是求逆序对的数目,那就要想到归并排序了。在归并过程中计算逆序对。
#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(),c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
//const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T; void testcase(){
printf("Case %d:",++T);
} const int MAXN = 5e5+ ;
const int MAXM = ;
const double eps = 1e-;
const double PI = acos(-1.0);
ll a[MAXN],tmp[MAXN];
ll ans;
int n; void Merge(int low,int mid,int high){
int i=low,j=mid+,k=low;
while(i<=mid&&j<=high){
if(a[i]<=a[j]){
tmp[k++]=a[i++];
}else{
ans += j-k;
tmp[k++] = a[j++];
}
}
while(i<=mid) tmp[k++] = a[i++];
while(j<=high) tmp[k++] = a[j++];
for(i=low;i<=high;++i){
a[i]=tmp[i];
}
}
void mergeSort(int a,int b){
if(a<b){
int mid =(a+b)>>;
mergeSort(a,mid);
mergeSort(mid+,b);
Merge(a,mid,b);
}
} int main() {
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
while(~scanf("%d",&n)&&n){
ans=;
for(int i=;i<n;i++) scanf("%lld",&a[i]);
mergeSort(,n-);
printf("%lld\n",ans);
}
return ;
}
POJ - 2299 Ultra-QuickSort(归并排序)的更多相关文章
- poj 2299 Ultra-QuickSort :归并排序求逆序数
		
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
 - 逆序数 POJ 2299 Ultra-QuickSort
		
题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...
 - 树状数组求逆序对:POJ 2299、3067
		
前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...
 - POJ 2299 Ultra-QuickSort(线段树+离散化)
		
题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...
 - POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
		
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
 - POJ 2299 【树状数组 离散化】
		
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
 - POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
		
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
 - POJ 2299 Ultra-QuickSort  归并排序、二叉排序树,求逆序数
		
题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...
 - poj  2299 Ultra-QuickSort  归并排序求逆序数对
		
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
 
随机推荐
- python+Selenium 环境搭建
			
一.下载相关软件 1.python http://python.org/getit/ 2.setuptools http://pypi.python.org/pypi/setuptools 3.pip ...
 - dokuwiki编辑器修改-color插件-添加按钮
			
需求 dokuwiki的编辑工具栏是以 MediaWiki 的为基础发展来的. 在它的编辑器color插件的颜色按钮中,我想添加新的按钮功能.如红色字体黄色背景的修饰,类似于涂中文字强调的意思. 步骤 ...
 - C++基础知识(2)
			
作为接口的函数头 C++函数可被其他函数激活或调用,函数头描述了函数与调用它的函数之间的接口. 在C语言中,省略返回类型相当于说函数的类型为int,然而,C++逐步淘汰了这种用法 也可以使用下面的变体 ...
 - [T-ARA][Ma boo]
			
歌词来源:http://music.163.com/#/song?id=22704447 作曲 : 金道勋/Rhymer [作曲 : 金道勋/Rhymer] 作词 : 金道勋 [作词 : 金道勋] 사 ...
 - IE=edge 让浏览器使用最新的渲染模式
			
Bootstrap不支持IE的兼容模式.为了让IE浏览器运行最新的渲染模式,建议将此 <meta> 标签加入到你的页面中: <metahttp-equiv="X-UA-Co ...
 - Spring sprint @ ninth day
			
时间 日期 地点 工作 20:05 5.20 九实 集成网络助手项目 遇到的困难:集成遇到,画了好久的rc文件,编译不了.rc文件也不能复制,还得重画.郁闷!!!
 - 四则运算APP最后阶段
			
四则运算APP最后阶段 [开发环境]:eclipse [开发项目]:小学生四则运算APP [开发人员]:郑胜斌 http://www.cnblogs.com/zsb1/ 孔德颖 http://www. ...
 - MongoDB给数据库创建用户
			
一.先以非授权的模式启动MongoDB 非授权: linux/Mac : mongod -f /mongodb/etc/mongo.conf windows : mongod --config c: ...
 - [转帖]LNMP组件安装
			
https://lnmp.org/install.html 系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu/Raspbian/Deepin/Aliyun/Amazon/M ...
 - vue 使用element-ui upload文件上传之后怎么清空
			
首先上传组件中一定要绑定这两个属性: ref,和 :file-list,如果没有ref,即使 用 this.$refs.upload.clearFiles()也不行,因为这时候this.$refs为空 ...