poj——2299(归并排序求逆序数对)
来源:互联网 发布:python手册中文版 pdf 编辑:程序博客网 时间:2024/05/29 17:40
题目地址:http://poj.org/problem?id=2299
参考地址:http://blog.csdn.net/lyy289065406/article/details/6647346
#include <iostream>#include <cmath>#include <string>#include <cstring>#include <cstdlib>#include <ctime>#include <algorithm>#include <cstdio>using namespace std;typedef long long ll;#define INF 1000000000#define MAX(a,b) a>b?a:b#define MIN(a,b) a>b?b:a#define N 500005 ll ans;void solve(int a[],int st,int mid,int end){ int len_L=mid-st+1;int len_R=end-mid;int* left=new int[len_L+2];int* right=new int[len_R+2];int i,j;for(i=1;i<=len_L;i++)left[i]=a[st+i-1];left[len_L+1]=INF; //设置无穷上界,避免比较大小时越界for(j=1;j<=len_R;j++)right[j]=a[mid+j];right[len_R+1]=INF; //设置无穷上界,避免比较大小时越界i=j=1;for(int k=st;k<=end;)if(left[i]<=right[j])a[k++]=left[i++];else{a[k++]=right[j++];ans+=len_L-i+1; //计算逆序数}delete left;delete right;return;}void mergesort(int a[],int st,int end){if(st<end){int mid=(st+end)/2;mergesort(a,st,mid);mergesort(a,mid+1,end);solve(a,st,mid,end);} return ;}int main(){int n; int a[N];while(scanf("%d",&n)!=-1&&n){ans=0;for(int i=1;i<=n;i++)cin>>a[i]; mergesort(a,1,n);printf("%I64d\n",ans);}return 0;}
- poj——2299(归并排序求逆序数对)
- poj 2299 归并排序求逆序数
- 求逆序数对(归并排序)
- poj(2299)——Ultra-QuickSort(归并排序求逆序数)
- poj-2299 Ultra—QuickSort(归并排序求逆序数)
- POJ 2299 Ultra-QuickSort(归并排序求逆序数)
- 2299 Poj Ultra-QuickSort(归并排序求逆序数)
- (POJ 2299)Ultra-QuickSort 归并排序求逆序数
- poj 2299 Ultra-QuickSort(归并排序求逆序对)
- poj 2299 Ultra-QuickSort——归并排序求逆序数,线段树离散化
- 归并排序和求逆序数对
- 归并排序(求逆序数对)
- 利用归并排序求逆序数对
- 利用归并排序求逆序数对
- poj 2188(归并排序求逆序对)
- POJ 归并排序求逆序对
- POJ 2299 逆序对(归并排序)
- 【nyoj117】—求逆序数(归并排序)
- Java中的throw和throws怎么用?
- OSPF之virtual-link
- poj1401--Factorial
- 利用栈实现括号匹配算法
- Android之系统自带的文字外观设置及实际显示效果图
- poj——2299(归并排序求逆序数对)
- JS通过相同的name进行表格求和
- 指针常量和常量指针
- linux下如何查看某个软件 是否安装??? 安装路径在哪???
- C++中引用传递与指针传递区别
- 数据库的基本概念
- Android中基本组件的复习
- (step4.3.4)hdu 1258(Sum It Up——DFS)
- 二叉树后序遍历(非递归)