POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数
题目链接: http://poj.org/problem?id=2299
题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数。
利用归并排序求解,内存和耗时都比较少, 但是有编码难度。。
二叉排序树,内存巨大,时间复杂度高,但是非常好写。。
归并排序版本:
#include <stdio.h>
#include <string.h>
long long merge_arr(int arr[], int first, int mid, int last, int tmp[])
{
long long ret = ;
int i = first, j = mid+, k = ;
while(i <= mid && j <= last)
{
if(arr[i] < arr[j])
{
tmp[k++] = arr[i++];
ret += j - mid - ;
}
else tmp[k++] = arr[j++];
}
while(i <= mid)
{
tmp[k++] = arr[i++];
ret += last - mid;
}
while(j <= last)
tmp[k++] = arr[j++];
for(i = ; i < k; i++)
arr[first+i] = tmp[i];
return ret;
} long long merge_sort(int arr[], int first, int last, int tmp[])
{
long long ret = ;
if(first < last)
{
int mid = (first + last) / ;
ret += merge_sort(arr, first, mid, tmp);
ret += merge_sort(arr, mid+, last, tmp);
ret += merge_arr(arr, first, mid, last, tmp);
}
return ret;
} int n, num[], tmp[];
int main()
{
while(scanf("%d", &n) != EOF && n)
{
for(int i = ; i < n; i++)
scanf("%d", &num[i]);
printf("%I64d\n", merge_sort(num, , n-, tmp));
}
return ;
}
二叉排序树版本:
#include <stdio.h>
#include <stdlib.h> struct node
{
int data, cnt_right;
struct node *left, *right;
}; long long ans; void build(struct node *&p, int k)
{
if(p == NULL)
{
p = (struct node *)malloc(sizeof(struct node));
p->data = k;
p->cnt_right = ;
p->left = p->right = NULL;
}
else if(p->data > k)
{
ans += p->cnt_right + ;
build(p->left, k);
}
else
{
p->cnt_right++;
build(p->right, k);
}
} void del(struct node *p)
{
if(p == NULL)return;
del(p->left);
del(p->right);
free(p);
} int main()
{
int n, x;
while(scanf("%d", &n) != EOF && n)
{
ans = ;
struct node *root = NULL;
while(n--)
{
scanf("%d", &x);
build(root, x);
}
del(root);
printf("%I64d\n", ans);
}
return ;
}
POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数的更多相关文章
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- POJ 2299 Ultra-QuickSort (排序+数据离散化+求顺序数)
题意:给出一个序列,求至少需要用到多少次操作,才能将序列从小到大排序. 思路: 数据量很大,n<500000,所以用冒泡模拟是超时的. 接下来就想到了求顺序数,总共需要交换的次数为每个数后面有多 ...
- 线段树菜鸟一题+归并排序【求逆序数】POJ2299
题目链接:http://poj.org/problem?id=2299 归并排序解法链接:http://blog.csdn.net/lyy289065406/article/details/66473 ...
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
- POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 39279 Accepted: 14163 ...
- 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(树状数组求逆序数)
链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...
随机推荐
- android的Broadcast receiver
broadcast receiver是用来监听intent的. android大量使用了broadcast receiver,比如:开机.电话打进来.发送消息,手机电量过低 有两种方式注册broadc ...
- [Javascript] The Array forEach method
Most JavaScript developers are familiar with the for loop. One of the most common uses of the for lo ...
- iOS 数组越界 Crash加工经验
我们先来看看有可能会出现的数组越界Crash的地方. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSInd ...
- Redis学习手册(开篇)
一.简介: 在过去的几年中,NoSQL数据库一度成为高并发.海量数据存储解决方案的代名词,与之相应的产品也呈现出雨后春笋般的生机.然而在众多产品中能够脱颖而出的却屈指可数,如Redis.MongoDB ...
- ltrace killed by SIGTRAP
[Ltrace-devel] ltrace stucks with pthreads Heiko Carstens heiko.carstens at de.ibm.comFri Apr 14 11: ...
- linux使用过程中遇到的问题和解决方法
测试过程中,出现以下错误,导致远程ssh连接不上,最终导致测试结果失败.系统日志如下: Sep 1 03:15:03 node3 avahi-daemon[5834]: Invalid respo ...
- Multipart Upload with HttpClient 4--reference
by Eugen Paraschiv on May 23, 2014 in HttpClient http://www.baeldung.com/httpclient-multipart-upload ...
- Top 10 questions about Java Collections--reference
reference from:http://www.programcreek.com/2013/09/top-10-questions-for-java-collections/ The follow ...
- vim乱码处理
编辑~/.vimrc文件,加上如下几行: set fileencodings=utf-8set termencoding=utf-8set encoding=utf-8
- html笔记05:html、css、javascript综合案例
1.首先是html代码: <!-- markup time --> <div class="wrapper wb"> <div class=" ...