Poj 2299 Ultra-QuickSort(归并排序)
题目链接:http://poj.org/problem?id=2299
思路分析:序列的逆序数即为交换次数,所以求出该序列的逆序数即可。根据分治法思想,序列分为两个大小相等的两部分,
分别求子序列的逆序数;对于右子序列中的每一个数,求出左序列中大于它的数的数目,计算的和即为解。另外,使用Merge排序时,
可以很容易求得对于右子序列中的每一个数,左序列中大于它的数的数目。
代码如下:
#include <stdio.h>
#include <limits.h> long long Count = ;
const int MAX_N = + ;
long long A[MAX_N], L[MAX_N], R[MAX_N]; void Merge(long long A[], int p, int q, int r)
{
int i, j, k; int n1 = q - p + ;
int n2 = r - q; for (int i = ; i < n1; ++i)
L[i] = A[p + i];
for (int j = ; j < n2; ++j)
R[j] = A[q + j + ]; i = j = ;
k = p;
L[n1] = INT_MAX;
R[n2] = INT_MAX; while (k <= r)
{
if (L[i] > R[j])
{
A[k++] = R[j++];
Count += n1 - i;
}
else
A[k++] = L[i++];
}
} void Merge_Sort(long long A[], int p, int q)
{
int r = (p + q) / ; if (p < q)
{
Merge_Sort(A, p, r);
Merge_Sort(A, r + , q);
Merge(A, p, r, q);
}
} int main()
{
int n; while (scanf("%d", &n) == )
{
if (n == )
break; Count = ; for (int i = ; i < n; ++i)
scanf("%d\n", &A[i]); Merge_Sort(A, , n - );
printf("%lld\n", Count);
} 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(归并排序)
https://vjudge.net/problem/POJ-2299 题意 求对于给定的无序数组,求出经过最少多少次相邻元素的交换之后,可以使数组从小到大有序. 分析 很明显是求逆序对的数目,那就要 ...
- 逆序数 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个数排成升序,需要交换几次? 解题 ...
随机推荐
- vs2010 项目编译 ResGen.exe”已退出,代码为 -1073741701
解决的办法如下: 1.关闭所有Visual Studio: 2.以管理员的身份打开命令提示窗口:(开始-运行-cmd) 3.更改目录至"CD C:\Program Files (x86)\M ...
- Windows API中几个函数的总结
[DllImport("User32.dll", EntryPoint = "FindWindow")] public static extern IntPtr ...
- zoj 3710 Friends
#include<stdio.h> #include<string.h> ][],h; int main(int argc, char* argv[]) { int t,i,n ...
- JavaMail收发邮件的一般流程与主要方法
1.Properties属性类 Properties p = new Properties(); p.put(key, value); key -| mail.smtp.host -| mail.sm ...
- 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本、内核、当前时间
申请博客有一段时间了,然而到现在还一篇没有写过..... 主要因为没有想到需要写些什么,最近在学习Python语言,照着书上看了看最基础的东西,发现根本看不进去,而且光看的话今天看了觉得都理解懂了,过 ...
- 通过读取excel数据和mysql数据库数据做对比(二)-代码编写测试
通过上一步,环境已搭建好了. 下面开始实战, 首先,编写链接mysql的函数conn_sql.py import pymysql def sql_conn(u,pwd,h,db): conn=pymy ...
- 17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment 引入额外的Slaves 到一个存在的复制
17.1.1.9 Introducing Additional Slaves to an Existing Replication Environment 引入额外的Slaves 到一个存在的复制环境 ...
- [用UpdateLayeredWindow实现任意异形窗口]
前面提到,我们可以用SetWindowRgn或SetLayeredWindowAttributes实现不规则以及半透明的效果 对于SetWindowRgn,它通过一个Rgn来设置区域,这个Rgn一般可 ...
- 使用回调接口实现ActiveX控件和它的容器程序的通讯
本文阅读基础:有一定的C++基础知识(了解继承.回调函数),对MFC的消息机制有一定了解,对COM的基础知识有一定了解,对ActiveX控件有一定了解. 一. 前言 ActiveX控件和它的容器程序如 ...
- HDU 1089 A+B for Input-Output Practice (I)
#include <cstdio> int main() { int a,b; while(scanf("%d%d",&a,&b)!=EOF) prin ...