poj 2299 树状数组 离散化
来源:互联网 发布:淘宝破损补寄险费用 编辑:程序博客网 时间:2024/06/05 17:45
树状数组求逆序数+离散化
求逆序数时,加入当前数字,然后统计大于当前数字的数字个数,
题目数据很大,需先将其离散化。
#include<cstdio>#include<cstring>#include<algorithm>#define N 500005#define lowbit(x) (x&-x)using namespace std;int c[N], a[N];struct Discre{int pos, v;}disc[N];void add(int k, int sum) {while (k <= N) {c[k] += sum;k += lowbit(k);}}int sum(int k) {int res = 0;while (k) {res += c[k];k -= lowbit(k);}return res;}bool cam(Discre &a, Discre &b) {return a.v < b.v;}int main() {int n;while (EOF != scanf("%d", &n), n) {memset(c, 0, sizeof(c));for (int i = 1; i <= n; i++) {scanf("%d", &disc[i].v);disc[i].pos=i;}sort(disc + 1, disc + n + 1, cam);//离散化前,需将其排序for (int i = 1; i <= n; i++)a[disc[i].pos] = i;//离散化过程long long ans = 0;for (int i = 1; i <= n; i++) {add(a[i], 1);ans += sum(n) - sum(a[i]);}printf("%lld\n", ans);}return 0;}
0 0
- POJ 2299 树状数组+离散化
- poj 2299 树状数组+离散化
- POJ 2299(树状数组,离散化)
- poj 2299 树状数组 离散化
- POJ-2299-树状数组,离散化
- POJ 2299 树状数组+离散化
- POJ 2299 离散化+树状数组
- POJ 2299 Ultra-QuickSort【树状数组+离散化】
- POJ 2299 离散化+树状数组求逆序数
- poj 2299 Ultra-QuickSort(离散化+树状数组)
- poj 2299 树状数组离散化逆序数 水
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
- POJ 2299 树状数组求逆序数+离散化
- poj 2299 离散化+树状数组求逆序数
- poj 2299 Ultra-QuickSort 离散化+树状数组
- POJ 2299 Ultra-QuickSort(树状数组+离散化)
- poj 2299(离散化+树状数组求逆序数)
- POJ 2299 Ultra-QuickSort(树状数组+离散化处理)
- Undefined property: view::$dom_id的解决方法
- 【那些年遇到过的面试题】 40个多线程问题总结
- iOS支持很多种键盘类型
- Java IO SequenceInputStream
- 离线地图解决方案
- poj 2299 树状数组 离散化
- 【CodeForces】〖 Educational Codeforces Round 16〗A. King Moves
- jsdom 使用
- Google map根据经纬度获取地址信息
- 深度学习笔记8:softmax层的实现
- 【机房收费】报表的制作
- JAVA基础总结一:
- Git操作、提交到GitHub等
- 计算机基础七之信息的浏览和发布