POJ
来源:互联网 发布:大富翁数据库 编辑:程序博客网 时间:2024/06/08 00:44
利用树状数组求逆序数,但是数据范围比较大,要使用离散化坐标
参考:http://blog.csdn.net/guhaiteng/article/details/52138756
// 树状数组求逆序数 + 离散化#include <cstdio>#include <string>#include <algorithm>#define MAX 500000 + 10using namespace std;typedef long long LL;typedef struct { LL val; LL pos;} Node;Node node[MAX];LL c[MAX];LL bit[MAX];LL lowbit( LL i ) { return i & ( -i );}void update( LL i, LL x, LL n ) { while( i <= n ) { bit[i] = bit[i] + x; i = i + lowbit( i ); }}LL getSum( LL i ) { LL ans = 0; while( i > 0 ) { ans = ans + bit[i]; i = i - lowbit( i ); } return ans;}bool cmp( Node a, Node b ) { return a.val < b.val ? 1 : 0;}int main() { LL n; while( scanf( "%lld", &n ) != EOF ) { if( n == 0 ) break; for( LL i = 1; i <= n; i++ ) { scanf( "%lld", &node[i].val ); node[i].pos = i; } sort( node + 1, node + 1 + n, cmp ); for( LL i = 1; i <= n; i++ ) { // 离散化 c[node[i].pos] = i; } // 初始化树状数组 for( LL i = 1; i <= n; i++ ) bit[i] = 0; LL ans = 0; for( int i = 1; i <= n; i++ ) { update( c[i], 1, n ); ans = ans + i - getSum( c[i] ); } printf( "%lld\n", ans ); } return 0;}
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 第一篇博客,记点简单的用法,oracle和mysql的转换
- python中List添加、删除元素的几种方法
- Socket网络协议
- Top 10 Performance Tuning Techniques for Amazon Redshift
- new delete 实现动态分配和释放二维数组
- POJ
- activeMq遇到的一些问题
- Win7安装msu文件失败引发的一系列问题
- 《利用Python进行数据分析》第二章-学习笔记
- 使用回调函数实现socket服务器端子线程刷新UI数据(socket通信第三弹)
- POJ 1001 Exponentiation 大浮点数乘法
- 解决Windows 7启动 Docker 报错
- lamp环境的搭建(源码包安装)
- Codeforces Round #374 (Div. 2) B. Passwords —— 基础题