POJ2299 Ultra-QuickSort 【树状数组】
来源:互联网 发布:什么叫数据流量 编辑:程序博客网 时间:2024/05/23 14:34
题目链接:http://poj.org/problem?id=2299
题意:求逆序对
先大赞一下markdown编辑器,今天知道了很多奥妙重重的功能。
听吉利说求逆序对还可以用树状数组?
于是激动的test了一发,吐槽一下,树状数组好像只能处理
代码:
// 树状数组离散化后求逆序对 #include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <map> // STL#include <string> #include <vector>#include <queue>#include <stack>#define mpr make_pair#define debug() puts("okkkkkkkk")using namespace std;typedef long long LL;const int inf = 1 << 26;struct nodes { int val, pos;} nds[1000005];int n, a[1000005], c[1000005];int cmp(nodes a, nodes b) { return a.val > b.val;}#define lowbit(i) i&(-i)void update(int x) { for( int i = x; i <= n; i += lowbit(i) ) ++ c[i];}int query(int x) { int ans = 0; for ( int i = x; i; i -= lowbit(i)) ans += c[i]; return ans;}int main(){ while( scanf("%d", &n), n ) { memset(c, 0, sizeof(c)); memset(nds, 0, sizeof(nds)); for ( int i = 1; i <= n; i ++ ){ scanf("%d", &nds[i].val); nds[i].pos = i; } sort(nds+1, nds+n+1, cmp); // 离散化 for ( int i = 1; i <= n; i ++ ) { if(nds[i].val != nds[i-1].val || i == 1) a[nds[i].pos] = n-i+1; else a[nds[i].pos] = a[nds[i-1].pos]; } LL ans = 0; for ( int i = 1; i <= n; i ++ ) { update(a[i]); ans += (LL)i-query(a[i]); } printf("%lld\n", ans); } return 0;}
吐槽一下,代码比归并排序还长,不过细节比较少,适合我这样的人….
阅读全文
0 0
- 【树状数组】 poj2299 Ultra-QuickSort
- POJ2299 Ultra-QuickSort(树状数组)
- poj2299 Ultra-QuickSort 树状数组
- POJ2299 Ultra-QuickSort 树状数组
- poj2299 Ultra-QuickSort 树状数组
- POJ2299 Ultra-QuickSort(树状数组)
- POJ2299 Ultra-QuickSort 【树状数组】
- POJ2299 Ultra-QuickSort 【树状数组】+【hash】
- POJ2299 Ultra-QuickSort【树状数组】【逆序数】
- poj2299 Ultra-QuickSort (树状数组+离散化)
- POJ2299 Ultra-QuickSort 树状数组+离散化
- poj2299 Ultra-QuickSort 二叉排序树或树状数组
- poj2299 Ultra-QuickSort 树状数组 + 离散化
- POJ2299 [Ultra-QuickSort] 值域树状数组
- POJ2299-Ultra-QuickSort(树状数组+离散化)
- POJ2299 Ultra-QuickSort(树状数组求逆序数)
- POJ2299:Ultra-QuickSort(树状数组求逆序数)
- POJ2299 Ultra-QuickSort(逆序数问题,树状数组求解)
- SpringMVC与Struts2区别与比较总结
- python3中数据抓取的三种方法
- VMware vSphere Web Services SDK编程指南(十)- 10.8 管理 VMFS 卷拷贝及诊断分区
- 20171001
- Hive命令行介绍
- POJ2299 Ultra-QuickSort 【树状数组】
- 海啸
- 线程锁
- jq代码学习10--事件冒泡
- 深入浅出MySQL笔记(三)
- AndroidGPS和基站定位
- 【图像融合】简单加权融合
- MySQL的备份
- Html中的label标签