题目

思路:先离散化数据然后树状数组搞一下求逆序数。

离散化的方法https://blog.csdn.net/gokou_ruri/article/details/7723378

自己对用树状数组求逆序数的理解:输入数据并利用树状数组求出前边比它小和等于它的数据有几个,用输入数据的总的个数减去比它小的数就是比它大的数res,将所有的res加起来就是要求的序列的逆序数。

如图:

把所有的res加起来就是答案了

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define FRE() freopen("in.txt","r",stdin)
#define INF 0x3f3f3f3f using namespace std;
typedef long long ll;
typedef pair<double,int> P;
const int maxn = ;
int n;
int a[maxn],b[maxn],tree[maxn]; int lowbit(int x)
{
return x & -x;
}
void Add(int x)
{
while(x <= n)
{
tree[x]++;
x += lowbit(x);
}
}
int getSum(int x)
{
int res = ;
while(x>)
{
res += tree[x];
x -= lowbit(x);
}
return res;
}
int main()
{
scanf("%d",&n);
for(int i = ; i < n; i++)
{
scanf("%d",&a[i]);
b[i] = a[i];
}
memset(tree,,sizeof(tree));
sort(b, b+n);
int len = unique(b, b+n) - b;
for(int i = ; i < n; i++)
{
a[i] = lower_bound(b, b + len, a[i]) - b + ;
}
ll sum = ;
for(int i = ; i < n; i++)
{
Add(a[i]);
sum += i + - getSum(a[i]);
}
printf("%I64d\n",sum);
return ;
}

SGU180 Inversions(树状数组求逆序数)的更多相关文章

  1. Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

                                                                    E. Infinite Inversions               ...

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

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

  3. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

  5. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  6. Codeforces645B【树状数组求逆序数】

    题意: 给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换. 求一个最大的逆序数. 思路: 感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数. #in ...

  7. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  8. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

随机推荐

  1. 关于HuffmanCoding的简单分析

    1.what's problem we faced? /** *    Q: what's problem we faced? * *    A: Data compression is still ...

  2. WinMain和MFC的差别

    API(Application Programming Interface):开放给应用程序调用的系统功能. 一个Windows Application(SDK): WinMain ReristerC ...

  3. Hdu5303 Delicious Apples 贪心

    题目链接: HDU5303 题意: 有一条环形的长为L的路,仓库在位置0处, 这条路上有n棵苹果树,给出每棵苹果树的位置和苹果数量, 问用 一次最多能装K个苹果的篮子   把这条路上全部苹果採回仓库最 ...

  4. new Modifier (C# Reference)

    https://msdn.microsoft.com/en-us/library/435f1dw2.aspx When used as a declaration modifier, the new  ...

  5. Android 体系结构介绍

    转自:http://blog.sina.com.cn/s/blog_4bc996c40100fawo.html 第一.操作系统层(OS)第二.各种库(Libraries)和Android 运行环境(R ...

  6. linux下.a/.so/.la目标库区别

    在linux平台上编译时,常会遇到目标库的疑问,有静态库也有动态库,单个理解都不太难,但是对复杂的工程而言,一旦混合到一起去,对整个工程的理解和调用,将会造成很大困扰,本文就汇总这几种常见编译结果文件 ...

  7. IE下元素设置百分比的问题

    场景:近两天在做一个控件,该控件是一个tab型的,并且该tab有可能是两个tab标签,也有可能是多个tab标签,为了能够适应这种动态需求, 在设置标签宽度的时候,直接用的最外层容器除以tab的个数,然 ...

  8. Akka源码分析-官方文档说明

    如果有小伙伴在看官方文档的时候,发现有些自相矛盾的地方,不要怀疑,可能是官方文档写错了或写的不清楚,毕竟它只能是把大部分情况描述清楚.开源代码一直在更新,官方文档有没有更新就不知道了,特别是那些官方不 ...

  9. Akka源码分析-Remote-位置透明

    上一篇博客中,我们研究了remote模式下如何发消息给远程actor,其实无论如何,最终都是通过RemoteActorRef来发送消息的.另外官网也明确说明了,ActorRef是可以忽略网络位置的,这 ...

  10. CSS------选择器-----------选择器的分组、属性选择器

    /*!--选择器的分组--*/ .groupDiv h1,h2,h3,h4{ color: #000000; } /*------------------------属性选择器--*/ [title] ...