http://poj.org/problem?id=2299

坑:答案是long long 输出……!!!!!

题意是:求一个数组进行冒泡排序交换的次数

题解:求逆序数

题解Ⅰ:

归并排序求逆序数

归并排序求逆序数之前写过

1.归并排序是把两个有序的数组合并成为一个有序的数组,利用分治的思想就可以进行排序

  逆序数可以利用这个思想求

  求出第一个数组的逆序数,和第二个数组的逆序数,再将两个数组整体的逆序数求出来

  f(x,y) = f(x,mid) + f(mid,y) + 之后数组的逆序数

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
const int MAXN = + ;
const int INF = 0x7fffffff;
const int MOD = ;
const double ESP = 10e-;
const double Pi = acos(-1.0);
typedef long long LL;
using namespace std;
LL a[MAXN];
LL b[MAXN];
LL h(int s,int e){
if(e-s <= ){
return ;
}
int mid = s + (e-s)/;
LL x = h(s,mid);
LL y = h(mid,e);
int p1 = s;
int p2 = mid;
int p3 = s;
LL cnt = ;
while(p1 < mid || p2 < e){
if(p2 >= e || (p1 < mid && a[p1] < a[p2])){
b[p3++] = a[p1++];
}
else{
b[p3++] = a[p2++];
cnt += (mid-p1); /*第二个数组当前元素比第一个数组当前元素小,所以第一个数组从当前元素到最后的元素都比第二个数组的大(数组一,二都已经有序了),所以第一个数组结尾下标,减去第一个数组的当前元素就是两个数组的逆序数*/
}
}
for(int i = s;i < e;i++){
a[i] = b[i];
}
return x+y+cnt;
}
int main(){
//freopen("input.txt","r",stdin);
int n;
while(~scanf("%d",&n) && n){
for(int i = ;i < n;i++){
scanf("%d",&a[i]);
}
LL ans = h(,n);
printf("%lld\n",ans);
}
return ;
}

题解Ⅱ:

http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html

这个童鞋写得已经很棒了
1.数组元素太大,而n 的个数又很少,所以需要离散化

离散化:

用struct Node{

  int v;

  int order;

};

将元素读入,并且将元素的次序读入

将元素排序之后,下标的标号一定程度上是代表元素的大小

所以用下标的标号就可以表示元素

这样范围就减少了

2.

树状数组是求 前 i 个 元素的和

先 add(a[i],1)

再 i - sum(a[i])

前面已经有 i 个元素了,sum(a[i]) 表示 1 - a[i] 的和 ,而 i - sum(a[i])  表示有多少个数字比 a[i] 大 但是 次序却在 i 位置的前面  就是 a[i] 元素的逆序数

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
const int MAXN = + ;
const int INF = 0x7fffffff;
const int MOD = ;
const double ESP = 10e-;
const double Pi = acos(-1.0);
typedef long long LL;
using namespace std;
int a[MAXN];
int bit[MAXN+];
int n;
struct Node{
int v;
int order;
bool operator < (const Node x)const{
return v < x.v;
}
};
Node in[MAXN];
int sum(int i){
int s = ;
while(i>){
s += bit[i];
i -= (i & -i);
}
return s;
}
void add(int i,int x){
while(i <= n){
bit[i] += x;
i += (i&-i);
}
}
int main(){
// freopen("input.txt","r",stdin);
while(~scanf("%d",&n) && n){
memset(bit,,sizeof(bit));
for(int i = ;i <= n;i++){
scanf("%d",&in[i].v);
in[i].order = i;
}
sort(in+,in++n);
for(int i = ;i <= n;i++){
a[in[i].order] = i;
}
LL ans = ;
for(int i = ;i <= n;i++){
add(a[i],);
ans += i-sum(a[i]);
}
printf("%lld\n",ans);
}
return ;
}

poj 2299 逆序数的更多相关文章

  1. POJ 2299 逆序对

    Crossings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description I ...

  2. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  3. 逆序数 POJ 2299 Ultra-QuickSort

    题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...

  4. poj 2299 Ultra-QuickSort (归并排序 求逆序数)

    题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...

  5. POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数

    题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...

  6. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  7. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  8. poj 2299 Ultra-QuickSort 归并排序求逆序数对

    题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...

  9. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

随机推荐

  1. 域用户允许更改IP地址

    1.在DC上设置不好使. 2.需在本地用户组里添加到network.......组里. 注意:有的时候,连接不到DC上,是由于DNS的事,需要先去掉不必要的dns地址. 添加完后,需要重启或注销. 另 ...

  2. PHP - 抓取电视剧资源

    <?php /** * 获取下载url * @return [type] [description] */ function getVedioDwonloadUrl() { for ($i=1; ...

  3. 内核必看: spinlock、 mutex 以及 semaphore

    linux 内核的几种锁介绍 http://wenku.baidu.com/link?url=RdvuOpN3RPiC5aY0fKi2Xqw2MyTnpZwZbE07JriN7raJ_L6Ss8Ru1 ...

  4. BZOJ 2134: 单选错位( 期望 )

    第i个填到第i+1个的期望得分显然是1/max(a[i],a[i+1]).根据期望的线性性, 我们只需将每个选项的期望值累加即可. ---------------------------------- ...

  5. python3.4.3将汉字转换为大写拼音首字母

    from pypinyin import pinyin a=pinyin(u'杨强',type=FIRST_LETTER)    --->此时返回一个列表并赋给a(元素也是列表) b=[]  - ...

  6. Ural 1197 - Lonesome Knight

    The statement of this problem is very simple: you are to determine how many squares of the chessboar ...

  7. .NET Core & ASP.NET Core 1.0

    .NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布 众所周知,Red Hat和微软正在努力使.NET Core成为Red Hat企业版Linux (RHEL) ...

  8. docker 学习笔记21:docker连接网络的设置

    1.如果docker主机不需要通过代理连接外网 则docker的相关命令(如docker search)或docker容器与网络相关的操作都可以正常进行,不需要特殊设置. 2.当docker主机 是通 ...

  9. 一步一步重写 CodeIgniter 框架 (12) —— 代码再重构,回归 CI

    第一课中搭建的基本的 框架模型, 只有一个 index.php 作为执行文件,按这种方式最不稳定的因素就是路径的问题. 我们经常需要通过合适的参数,比如 load_class('output') 或 ...

  10. iOS开发- 获取精确剩余电量

    [UIDevice currentDevice].batteryMonitoringEnabled = YES; double deviceLevel = [UIDevice currentDevic ...