poj 2299 求逆序对
来源:互联网 发布:怎样开个淘宝网店 编辑:程序博客网 时间:2024/05/01 12:56
经典题目: 给定一个序列,求其中的逆序对
在归并排序(merge sort)的基础上统计,时间复杂度O(nlogn)
注意使用long long
#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 = 10000000;const int MAXN = 500100;const int maxn = MAXN;///全局变量 和 函数//int T;int n;long long A[maxn], T[maxn];long long cnt;long long quick_sort(long long *A, long long x, long long y, long long *T){ long long cnt = 0; if(y - x > 1) { long long m = x + (y - x) / 2; long long p = x, q = m, i = x; long long leftt = quick_sort(A, x, m, T); long long rigtt = quick_sort(A, m, y, T); while(p < m || q < y) { if(q >= y || (p < m && A[p] <= A[q])) { T[i++] = A[p++]; } else { T[i++] = A[q++]; cnt += m - p; //添加这一句即可,其它同归并排序 } } for(i = x; i < y; i++) A[i] = T[i]; cnt += leftt; cnt += rigtt; } return cnt;}int main(){///变量定义int i, j; while(scanf("%d", &n) == 1 && n) { for(i = 0; i < n; i++) scanf("%lld", &A[i]); printf("%lld\n", quick_sort(A, 0, n, T)); }///结束return 0;}
- poj 2299 求逆序对
- poj 2299 求逆序对
- poj 2299 1804 2085 求逆序对
- POJ 2299 Ultra-QuickSort(求逆序对)
- poj 2299 Ultra-QuickSort 求逆序对
- poj 2299 Ultra-QuickSort(求逆序对)
- poj 2299 Ultra-QuickSort(求逆序对)&& poj 1804
- poj 2299 求逆序对 树状数组 归并排序
- poj 2299 Ultra-QuickSort(归并排序求逆序对)
- poj 2299 Ultra-QuickSort(fenwick树求逆序对)
- POJ 2299 求逆序对个数 归并排序 Or数据结构
- POJ 2299 <离散化+树状数组求逆序数对>
- POJ 2299 Ultra-QuickSort 快速排序求逆序对
- poj 2299 归并排序求逆序对 解题报告
- POJ 2299 Ultra-QuickSort(树状数组求逆序对)
- POJ-2299Ultra-QuickSort(BIT|归并排序求逆序对)
- POJ 2299(树状数组求逆序对)
- POJ 2299 Ultra-QuickSort 模板 求逆序对
- shell中if判断的参数
- git提交方式
- IT的成功人士学习之道
- Playlist codeforces 268E
- Playfair密码变种加密
- poj 2299 求逆序对
- 自己动手编写一个VS插件(一)
- iOS中永久保存用户数据的两个地方
- 情感词生成
- free与delete的区别
- 蝴蝶效应---。。。
- C语言---变长参数列表---变长参数的使用
- FileItem的常用方法
- oracle 11g dataguard