1009. Triple Inversions (35)
来源:互联网 发布:巴洛克记忆音乐知乎 编辑:程序博客网 时间:2024/05/21 10:58
IDEA
1.使用树状数组求逆序数的问题,参考 http://wenku.baidu.com/view/8251fc0516fc700abb68fc76.html?re=view 点击打开链接
2.以中间数为基准,算出左边比其大的数个数,右边比其小的数个数,二者相乘得出结果
3.结果用long long表示
CODE
#include<iostream>#include<vector> #include<queue>#include<algorithm>#include<fstream>using namespace std;#define MAX 100005int n;int a[MAX],c[MAX]={0};long long l[MAX],r[MAX];int lowbit(int x){return x&-x;}void add(int x){while(x<=n){c[x]++;x+=lowbit(x);}}int sum(int x){int s=0;while(x>0){s+=c[x];x-=lowbit(x);}return s;}int main(){#ifndef ONLINE_JUDGEfreopen("input.txt","r",stdin);#endifcin>>n;for(int i=1;i<=n;i++){cin>>a[i];}for(int i=1;i<=n;i++){add(a[i]);l[i]=i-sum(a[i]);//i为当前已经插入数的个数,sum(a[i])为比a[i]小的数的个数,i-sum(a[i])即为比 比a[i]大的数的个数,即逆序个数 }for(int i=1;i<=n;i++){c[i]=0;}//for(int i=1;i<=n;i++){//add(a[i]);//r[i]=a[i]-sum(a[i]);//}for(int i=n;i>=1;i--){r[i]=sum(a[i]);add(a[i]);}long long count=0;for(int i=1;i<=n;i++){count+=l[i]*r[i];}cout<<count<<endl;#ifndef ONLINE_JUDGEfclose(stdin);#endifreturn 0;}
0 0
- 1009. Triple Inversions (35)
- 1009. Triple Inversions (35)
- 1009. Triple Inversions (35)
- 1009. Triple Inversions (35)解题报告
- pat-top 1009. Triple Inversions (35)
- PAT-TL 1009. Triple Inversions
- PAT (Top Level) Practise 1009 Triple Inversions (35)
- Inversions
- triple
- Triple
- hdu 5196 DZY Loves Inversions && BestCoder Round #35
- sgu180:Inversions
- Triple Buffering
- hdu517 Triple
- BZOJ3771: Triple
- BZOJ3771 Triple
- bzoj3771 Triple
- BZOJ3771: Triple
- pod install --no-repo-update
- 用PL/SQL连接oracle遇到的问题及解决方法:ORA-12154:TNS:could not resolve the connect identifier specified。。
- POJ_1005_I Think I Need a Houseboat
- Python基础之矩阵输入
- leetcode试题总结<8>
- 1009. Triple Inversions (35)
- windows消息机制
- 信号量与互斥、阻塞与非阻塞的概念
- ROS学习笔记九:用C++编写ROS发布与订阅
- C++中的IO类(iostream, fstream, stringstream)小结
- Android源码分析- View系统解析
- 【u015】兽径管理
- leetcode 27. Remove Element (EASY)
- 关于HashCode与其系数31