poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接:
http://poj.org/problem?id=2299
题目描述:
给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次?
解题思路:
根据冒泡排序的特点,我们可知,本题只需要统计每一个数的逆序数(如果有i<j,存在a[i] > a[j],则称a[i]与
a[j]为逆序数对),输出所有的数的逆序数的和用普通排序一定会超时,但是比较快的排序,像快排又无法统计
交换次数,这里就很好地体现了归并排序的优点。典型的利用归并排序求逆序数。
归并排序:比如现在有一个序列[l,r),我们可以把这个序列分成两个序列[l,mid),[mid,r),利用递归按照上
述方法逐步缩小序列,先使子序列有序,再使子序列区间有序,然后再把有序区间合并,很好滴体现了分治的思想。
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
#define maxn 500010 int a[maxn], b[maxn];
long long count; void merge (int l, int r);
int main ()
{
int n, i;
while (scanf ("%d", &n), n)
{
memset (a, , sizeof (a));
memset (b, , sizeof (b));
for (i=; i<n; i++)
scanf ("%d", &a[i]);
count = ;//一定要用int64,int32会溢出
merge (, n);
printf ("%lld\n", count);
}
return ;
} void merge (int l, int r)//归并排序,参数分别是子区间的位置
{
if (r - l <= )
return ;
int mid = l + (r - l) / ;
merge (l, mid);
merge (mid, r);
int x = l, y = mid, i = l;
while (x<mid || y<r)//对子序列进行排序,并且存到数组b里面
{
if (y >= r || (x < mid && a[x] <= a[y]))
b[i ++] = a[x ++];
else
{
if (x < mid)//记录交换次数
count += mid - x;
b[i ++] = a[y ++];
}
}
for (i=l; i<r; i++)//把排好序的子序列抄到a数组对应的位置
a[i] = b[i];
}
poj 2299 Ultra-QuickSort 归并排序求逆序数对的更多相关文章
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- poj 2299 树状数组求逆序数+离散化
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...
- POJ 2299 -Ultra-QuickSort-树状数组求逆序数
POJ 2299Ultra-QuickSort 使用树状数组记录逆序对数. 把数组按照大小顺序插入,getsum(i)就是i前面的比他大的数. #include <cstdio> #inc ...
- [CF 351B]Jeff and Furik[归并排序求逆序数]
题意: 两人游戏, J先走. 给出一个1~n的排列, J选择一对相邻数[题意!!~囧], 交换. F接着走, 扔一硬币, 若正面朝上, 随机选择一对降序排列的相邻数, 交换. 若反面朝上, 随机选择一 ...
- POJ2299 Ultra-QuickSort(归并排序求逆序数)
归并排序求逆序数 Time Limit:7000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descri ...
- HDU 3743 Frosh Week(归并排序求逆序数)
归并排序求逆序数 #include <iostream> #include <cstdio> using namespace std; #define maxn 1000005 ...
- hiho一下 第三十九周 归并排序求逆序数
题目链接:http://hihocoder.com/contest/hiho39/problem/1 ,归并排序求逆序数. 其实这道题也是可以用树状数组来做的,不过数据都比较大,所以要离散化预处理一下 ...
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
随机推荐
- 【APUE】进程基础
进程标识符:非负整数 ID为0的进程通常是是调度进程,常被称为交换进程.该进程是内核的一部分,它并不执行任何磁盘上的程序,因此也被称为系统进程 ID为1的进程是init进程,在自举过程结束时由内核调用 ...
- IntelliJ 中类似于Eclipse ctrl+o的是ctrl+F12
IntelliJ 中类似于Eclipse ctrl+o的是ctrl+F12 学习了:https://blog.csdn.net/sjzylc/article/details/47979815
- Deepin-安装git
sudo apt-get install git 命令介绍(安装软件):apt-get install 命令介绍(Debian系列以管理员运行的前缀):sudo
- 有两个字符串a,b。假设a="ab",b="cd",判断字符串c="acbd"是属于a、b的组合。满足组合后a、b的内部顺序均不变。
#include<iostream> #include<string> using namespace std; int check(string a,string b,str ...
- 在XX公司工作第二天,维护已有代码
根据<C++ More Exception>所述的规则: Rule #1: Never write using-directives in header files. Rule #2: N ...
- Android5.0(lollipop)新特性介绍(一)
今年6月的Google I/O大会上.Android L的初次见面我相信让会让非常多android粉丝有些小激动和小期待.当然作为开发人员的我来说,激动不言而喻,毕竟这是自08年以来改变最大的一个版本 ...
- leetCode(26):Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Intellig Idea2017新建Web项目(tu'wen)
1.新建新工程项目 2.选择Java 和JDK版本 3.下一步Next(默认不勾选) 4.设置Project Name ,点击More Setting图标可以折叠.展开 然后Finish 我们可以看 ...
- 在spring中映射X.hbm.xml文件的小技巧
通常在spring中会这么写代码: <bean id="sessionFactory" class="org.springframework.orm.hiberna ...
- Tomcat 安装错误
安装tomcat时,遇到"failed to install tomcat6 service check your settings and permissions"的问题 安装时 ...