SGU180:Inversions(树状数组)
来源:互联网 发布:网络推广总监 编辑:程序博客网 时间:2024/06/06 12:25
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
题意:
求逆序数
#include <iostream>#include <stdio.h>#include <string.h>#include <string>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <math.h>#include <bitset>#include <list>#include <algorithm>#include <climits>using namespace std;#define lson 2*i#define rson 2*i+1#define LS l,mid,lson#define RS mid+1,r,rson#define UP(i,x,y) for(i=x;i<=y;i++)#define DOWN(i,x,y) for(i=x;i>=y;i--)#define MEM(a,x) memset(a,x,sizeof(a))#define W(a) while(a)#define gcd(a,b) __gcd(a,b)#define LL long long#define N 67000#define INF 0x3f3f3f3f#define EXP 1e-8#define lowbit(x) (x&-x)const int mod = 1e9+7;LL c[N],n,tot,r[N];struct node{ LL x,s,id;} a[N];int cmp(node a,node b){ if(a.x!=b.x) return a.x<b.x; return a.id<b.id;}LL sum(LL x){ LL ret = 0; while(x>0) { ret+=c[x]; x-=lowbit(x); } return ret;}void add(LL x,LL d){ while(x<=n) { c[x]+=d; x+=lowbit(x); }}int main(){ LL i,j,k; while(~scanf("%lld",&n)) { MEM(c,0); for(i = 1; i<=n; i++) { scanf("%lld",&a[i].x); a[i].id = i; } sort(a+1,a+1+n,cmp); for(i = 1; i<=n; i++) { r[a[i].id] = i; } LL ans = 0; for(i = 1; i<=n; i++) { add(r[i],1); ans+=(i-sum(r[i])); } printf("%lld\n",ans); } return 0;}
0 0
- SGU180:Inversions(树状数组)
- sgu180 Inversions(树状数组+离散化)
- sgu180:Inversions
- SGU180——Inversions
- SGU 180. Inversions 离散 + 树状数组
- URAL 1523.K-inversions (树状数组)
- SGU 180 Inversions (树状数组+离散化)
- SGU 180-Inversions(树状数组离散化求逆序对数)
- ural 1523 K-inversions(dp+树状数组)
- SUG 180 Inversions(树状数组+离散化)
- HDU 5196 DZY Loves Inversions(树状数组,二分)
- Codeforces 540E Infinite Inversions 离散化+树状数组
- 树状数组Codeforces Round #301 (Div. 2) E - Infinite Inversions
- codeforces 540 E. Infinite Inversions (离散化 + 树状数组)
- SGU 180 Inversions(树状数组求逆序对)
- SGU 180 Inversions <一维树状数组区间查询>
- CodeForces-540E Infinite Inversions(树状数组+离散化+加点)
- sgu180
- mybatis 模糊查询报错: java.sql.SQLException: ORA-01006: 绑定变量不存在
- JScrollPane中添加JPanel不出现滚动条
- android开发 获取logcat日志并记录(方便离线调试)
- 嫌工资少,先来看看你值多少钱
- mysql 删除语句几种常见的用法
- SGU180:Inversions(树状数组)
- jQuery的$("#id")和document.getElementById("id")的区别
- 做电子商务网站建设是的小技巧
- Android中实现远程输入
- 联网查看图片的Demo
- 使用windbg調試iis應用程序池崩溃的asp.net 2.0 bug.
- 使用CSS隐藏HTML元素的4种常用方法
- js小结
- 请问,activemq跟websocket什么区别?