poj 2299 求逆序数
#include <iostream>
const int MAX = ;
int a[MAX];
int swap[MAX]; //临时数组
int n; //数组a的长度
__int64 result; //数组a中的逆序数 //归并两个已经有序的段:a[low]—a[mid]和a[mid+1]—a[high],使得a[low]—a[high]有序。
void merge(int low, int mid, int high)
{
int i = low;
int j = mid + ;
int m = ;
while(i <= mid && j <= high)
{
if(a[i] <= a[j])
{
swap[m++] = a[i];
i++;
}
else
{
swap[m++] = a[j];
j++;
result += mid - i + ;
}
}
while(i <= mid)
{
swap[m++] = a[i];
i++;
}
while(j <= mid)
{
swap[m++] = a[j];
j++;
}
for(i = ; i < m; i++)
a[low + i] = swap[i];
} //归并排序:对a[low]—a[high]进行归并排序。
void mergeSort(int low, int high)
{
int mid;
if(low < high)
{
mid = (low + high) /;
mergeSort(low, mid);
mergeSort(mid + , high);
merge(low, mid, high);
}
} int main()
{
int i;
while(true)
{
scanf("%d",&n);
if(n == ) break;
result = ;
for(i = ; i < n; i++)
scanf("%d",a+i);
mergeSort(, n-);
printf("%I64d\n",result);
}
return ;
}
poj 2299 求逆序数的更多相关文章
- POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树
题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...
- POJ 2299 求逆序对个数 归并排序 Or数据结构
题意: 求逆序对个数 没有重复数字 线段树实现: 离散化. 单点修改,区间求和 // by SiriusRen #include <cstdio> #include <cstring ...
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
- POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数
题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数)
链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 70674 Accepted: 26538 ...
- 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
随机推荐
- 无DLL远程注入
界面如下: 主要代码如下: #define STRLEN 20 typedef struct _DATA { DWORD dwLoadLibrary; DWORD dwGetProcAddress; ...
- WindowsPhone使用HtmlAgilityPack解析HTML
NuGet里添加HtmlAgilityPack的引用 然后wp上使用必须添加本地 C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libr ...
- nginx负载均衡配置一(反向代理)
一.前提 1:系统linux(centos) 2:nginx代理服务器(web:192.168.1.10 proxy.abc.com) 3:nginx后台服务器(web1:192.168.1.11 ...
- php-resque学习笔记二(配置)
1:前提 系统:CentOS PHP版本:PHP7 安装目录:/usr/local/php php-resque 安装目录:/home/software/php-resque ...
- Sqlserver2005附加数据库为只读的解决方法
在sqlserver2005中附加数据库时,附加的数据库会变成只读的,只能进行查询操作. 解决方法 1 打开SqlServer Configuration Manager 开始->Micro ...
- winform的datagridview单元格输入限制和右键单击datagridview单元格焦点跟着改变
在datagridview的EditingControlShowing事件里面添加代码: if (this.dgv_pch.Columns[dgv_pch.CurrentCell.ColumnInde ...
- [leetcode]_Pascal's Triangle II
题目:Pascal三角的变形,要求只用O(K)的额外空间. 思路:由于Pascal三角中,tri[n][i] = tri[n - 1][i] + tri[n-1][i-1],(通常情况下) 如果已经获 ...
- Vue.js学习 Item6 -- Class 与 样式绑定
数据绑定一个常见需求是操作元素的 class 列表和它的内联样式.因为它们都是 attribute,我们可以用 v-bind 处理它们:只需要计算出表达式最终的字符串.不过,字符串拼接麻烦又易错.因此 ...
- 重拾C,一天一点点_10
来博客园今天刚好两年了,两年前开始学编程. 忙碌近两个月,项目昨天上线了,真心不容易,也不敢懈怠,接下来的问题会更多.这两天调试服务器,遇到不少麻烦. 刚出去溜达了一下,晚上天凉了,现在手感觉凉的有点 ...
- 在xml中添加array
在values建立arrays(名字可自定义)的xml: <?xml version="1.0" encoding="utf-8"?> <re ...