SGU 180-Inversions(树状数组离散化求逆序对数)
来源:互联网 发布:eclipse mac安装 编辑:程序博客网 时间:2024/05/16 19:04
180. Inversions
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
memory limit per test: 4096 KB
input: standard
output: standard
output: standard
There are N integers (1<=N<=65537) A1, A2,.. AN (0<=Ai<=10^9). You need to find amount of such pairs (i, j) that 1<=i<j<=N and A[i]>A[j].
Input
The first line of the input contains the number N. The second line contains N numbers A1...AN.
Output
Write amount of such pairs.
Sample test(s)
Input
5 2 3 1 5 4
Output
3
[submit]
[forum]
Author:Stanislav AngelyukResource:Saratov ST team Spring Contest #1Date:18.05.2003
/***************************** author:crazy_石头* date:2014/01/15* algorithm:BIT* Pro:SGU180-inversions***************************/#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>#include <algorithm>#include <queue>#include <map>#include <string>using namespace std;#define INF 1<<29#define eps 1e-8#define A system("pause")#define rep(i,h,n) for(int i=(h);i<=(n);i++)#define ms(a,b) memset((a),(b),sizeof(a))#define LL __int64const int maxn=70000+10;struct Node{ LL id,num;}e[maxn];LL C[maxn],n,h[maxn];inline bool cmp(Node E,Node D){ return E.num<D.num;}inline int lowbit(int x){ return x&-x;}inline void update(int x,int d){ while(x<=n) { C[x]+=d; x+=lowbit(x); }}inline LL getsum(int x){ LL ret=0; while(x>0) { ret+=C[x]; x-=lowbit(x); } return ret;}int main(){ while(~scanf("%d",&n)) { ms(C,0); rep(i,1,n) { scanf("%I64d",&e[i].num); e[i].id=i; } sort(e+1,e+n+1,cmp); h[e[1].id]=1; int cnt=1; rep(i,2,n) { if(e[i].num!=e[i-1].num) cnt++; h[e[i].id]=cnt; } LL sum=0; rep(i,1,n) { update(h[i],1); sum+=i-getsum(h[i]); } printf("%I64d\n",sum); } return 0;}
0 0
- SGU 180-Inversions(树状数组离散化求逆序对数)
- SGU 180 Inversions (树状数组+离散化)
- 离散化+树状数组 求逆序对数
- SGU 180 Inversions(树状数组求逆序对)
- SGU 180. Inversions 离散 + 树状数组
- poj2299 Ultra-QuickSort&&NYOJ117 求逆序数 (树状数组求逆序对数+离散化)+(归并排序)
- POJ-2299 Ultra-QuickSort (树状数组 离散 求逆序对数)
- Ultra-QuickSort 求最小交换次数即求逆序对数 树状数组+离散化
- (POJ 2299)Ultra-QuickSort 树状数组求逆序对数 + 离散化
- HDU 4911 Inversion(树状数组求逆序对数 + 数据离散化)
- 树状数组求逆序对数
- 《树状数组求逆序对数》
- 离散化+树状数组求逆序数
- 树状数组+离散化求逆序对
- poj2299(离散化+树状数组求逆序)
- 树状数组离散化求逆序数
- 树状数组+ 离散化 求逆序数
- sgu 180. Inversions (树状数组+离散化,第一道需要改模板的题目,好题)
- c 面试题
- Ubuntu 进入单用户模式—修改启动项利器
- GWT的默认窗体控件,在右上角增加关闭按钮
- 蓝桥杯 【入门训练】 序列求和
- 在WIN7 64位下的汇编环境搭建
- SGU 180-Inversions(树状数组离散化求逆序对数)
- 统计程序中空格,制表符,换行符的个数
- Javac 用法记录
- Myeclipse创建Web项目
- Android上用speex做回音消除
- hadoop,hbase,mongodb多节点集群安装入门教程以及ycsb测试性能
- ZOJ-2420
- 软件测试
- POJ3468 A Simple Problem with Integers 线段树 区间成段更新+区间求和