poj 2488 排序交换数
来源:互联网 发布:淘宝定制t恤 编辑:程序博客网 时间:2024/06/12 19:49
直接模拟冒泡果断TLE
后来看别人的解释说采用归并排序
归并在逆序对计算中的便捷在于
合并过程中如果A数组中X大于B数组中Y,那么X后面的数字与Y都构成逆序对
#include <iostream>#include <vector>#include <map>#include <list>#include <set>#include <deque>#include <stack>#include <queue>#include <algorithm>#include <cmath>#include <cctype>#include <cstdio>#include <iomanip>#include <cmath>#include <cstdio>#include <iostream>#include <string>#include <sstream>#include <cstring>#include <queue>using namespace std;///宏定义const int INF = 20000000;const int MAXN = 500050;///全局变量 和 函数long long numbers[MAXN], t[MAXN];long long n;long long tot;//bool flag;//void Merge(int l, int m, int r){int p = 0;int i = l; int j = m + 1;while (i <= m && j <= r){//如果存在逆序对if (numbers[i] > numbers[j]){t[p++] = numbers[j++];tot += m - i + 1;//后面所有的都是逆序对}else{t[p++] = numbers[i++];}}while(i <= m) t[p++] = numbers[i++];while(j <= r) t[p++] = numbers[j++];for (i = 0; i < p; i++){numbers[l + i] = t[i];}}void MergeSort(int l, int r){int m;if (l < r){m = (l + r) / 2;MergeSort(l, m);MergeSort(m + 1, r);Merge(l, m, r);}}int main(){///变量定义int i, j;while (1){scanf("%lld", &n);if (n == 0)break;for (i = 0; i < n; i++)scanf("%lld", &numbers[i]);tot = 0;MergeSort(0, n - 1);printf("%lld\n", tot);}///结束return 0;}
- poj 2488 排序交换数
- 指针交换三个数 排序输出
- 交换数
- javascript冒泡排序方法并统计相邻数交换次数
- 指针实现—三个数交换的排序
- UVA331求排序过程交换次数最少的种类数
- 178_冒泡排序的交换次数(逆序数)
- C用交换函数实现三个数排序
- 冒泡排序以及两个数之间的交换
- poj 1654--重新排序求交换的次数
- poj 2299 交换的最少次数 归并排序
- POJ-2299 Ultra-QuickSort-分治法排序求交换速度
- 交换排序
- 交换排序
- 交换排序
- 交换排序
- 交换排序
- 交换排序
- 英文NLP工具包推荐
- C语言源码的解析
- ORACLE 10g下载|ORACLE 10g下载地址|ORACLE 10g官网下载地址
- [源码分享]等比例压缩图片
- 获取number数据的类型
- poj 2488 排序交换数
- Win2008 R2下用Hyper-V管理器无法连接Hyper-V 虚拟机的问题
- com.taobao.api.ApiException: java.net.ConnectException: Connection refused: connect错误
- HTML5的明天, 局部有小雨
- ExtJS layout的9种样式详解(一)
- 【小蒙淘金】12.12金评-贵金属行情分析及部分操作建议
- html标签在xml里面的表示--使用实体
- alert,confirm弹出窗样式美化
- ExtJS layout的9种样式详解(二)