POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】

时间:2023-01-08 17:06:40

题意:给出一组数,然后求它的逆序数

先把这组数离散化,大概就是编上号的意思---

然后利用树状数组求出每个数前面有多少个数比它小,再通过这个数的位置,就可以求出前面有多少个数比它大了

这一篇讲得很详细

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

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int n;
int a[maxn];
int bit[maxn];//树状数组 struct node{
int x,id;
} p[maxn]; int cmp(node n1,node n2){
return n1.x < n2.x;
} int lowbit(int x){ return x & (-x);} void add(int i, int x){
while(i <= n){
bit[i]+=x;
i += lowbit(i);
}
} int sum (int i){
int s=;
while ( i > ){
s += bit[i];
i -= lowbit(i);
}
return s;
} int main(){
while(scanf("%d",&n),n){
for(int i=;i<=n;i++){
scanf("%d",&p[i].x);
p[i].id=i;
}
sort(p+,p+n+,cmp);
for(int i=;i<=n;i++) a[p[i].id] = i; memset(bit, , sizeof(bit));
LL ans=;
for(int i=; i <= n; i++) {
add(a[i],);
ans += i - sum(a[i]);
}
printf("%I64d\n",ans);
}
return ;
}

树状数组的第一道题目---------------加油-------

gooooooooooooo--------------------------------------

POJ 2299 Ultra-QuickSort【树状数组 ,逆序数】的更多相关文章

  1. Poj 2299 - Ultra-QuickSort 离散化&comma;树状数组&comma;逆序对

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 52306   Accepted: 19194 ...

  2. poj 2299 Ultra-QuickSort(树状数组求逆序数&plus;离散化)

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

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

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

  4. POJ 2299 Ultra-QuickSort(树状数组&plus;离散化)

    http://poj.org/problem?id=2299 题意:给出一组数,求逆序对. 思路: 这道题可以用树状数组解决,但是在此之前,需要对数据进行一下预处理. 这道题目的数据可以大到999,9 ...

  5. POJ - 2299 Ultra-QuickSort 【树状数组&plus;离散化】

    题目链接 http://poj.org/problem?id=2299 题意 给出一个序列 求出 这个序列要排成有序序列 至少要经过多少次交换 思路 求逆序对的过程 但是因为数据范围比较大 到 999 ...

  6. poj 2299 Ultra-QuickSort(树状数组)

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 67681   Accepted: 25345 ...

  7. HDU 4911 &lpar;树状数组&plus;逆序数&rpar;

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  8. HDU5196--DZY Loves Inversions 树状数组 逆序数

    题意查询给定[L, R]区间内 逆序对数 ==k的子区间的个数. 我们只需要求出 子区间小于等于k的个数和小于等于k-1的个数,然后相减就得出答案了. 对于i(1≤i≤n),我们计算ri表示[i,ri ...

  9. hdu2838Cow Sorting(树状数组&plus;逆序数)

    题目链接:点击打开链接 题意描写叙述:给定一个长度为100000的数组,每一个元素范围在1~100000,且互不同样,交换当中的随意两个数须要花费的代价为两个数之和. 问怎样交换使数组有序.花费的代价 ...

随机推荐

  1. WinForm 代码实现以管理员身份运行

    [STAThread] static void Main(string[] Args) { //获得当前登录的Windows用户标示 System.Security.Principal.Windows ...

  2. IOS开发基础知识--碎片46

    1:带中文的URL处理 // http://static.tripbe.com/videofiles/视频/我的自拍视频.mp4 NSString *path = (__bridge_transfer ...

  3. I&&num;39&semi;m an artist who loves linux &lpar;转&rpar;

    My father got me a computer for graduation with 512MB RAM and a Pentium processor. It came with Wind ...

  4. 检测PC端和移动端的方法总结(转)

    正在苦逼的实习中,昨天公司让做一个页面,涉及到检测终端的问题,如果是手机设备,就跳转到指定的网页上,以前写响应式布局只要用@media screen来实现布局的差异化适应,但是现在不仅仅是布局,还要针 ...

  5. BZOJ 4551 树

    线段树+标记永久化. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  6. php面向对象的基础

    这是第一次写博客,希望大家多多支持! 一.OOP概念 1.类(class) 它包括名称.方法.属性和事件.实际是它本身不是对象,因为它不存在内存中.当引用类的代码运行时,类的一个新的实例,及对象,就在 ...

  7. JS 冒泡排序从学到优化

    目的:理解算法 深化算法 冒泡排序: 直接上动图好于文字 一个冒泡实例 45,67,23,88,21,6,99// 第一轮 6次// 45 67 23 88 21 6 99// 45 23 67 88 ...

  8. 有两个表A和B,均有key和value两个字段,如果B的key在A中也有,就把B的value替换为A中对应的value

    update B b set b.value=(select max(a.value) from A a where b.key=a.key) from A c where b.key=c.key) ...

  9. 朱晔的互联网架构实践心得S1E3:相辅相成的存储五件套

    朱晔的互联网架构实践心得S1E3:相辅相成的存储五件套 [下载本文PDF进行阅读] 这里所说的五件套是指关系型数据库.索引型数据库.时序型数据库.文档型数据库和缓存型数据库. 上图显示了一套读写服务搭 ...

  10. 用canvas画三角形的方法

    <canvas id="favoriteRectangle" width="30" height="30"></canva ...