poj2299(树状数组+离散化)
这道题题意很简单,就是求逆序数。用暴力的方法是显然会超时的。这里考虑采用树状数组。
采用树状数组话遇到的问题就是需要999,999,999个空间来存放数据,这显然是不可行的。考虑到输入数据最多只有500,000个,那么可以采用离散化的方法来先将输入数据进行映射到较小的空间上,然后再用一般的树状数组操作统计即可。
#include <cstdio>
#include <algorithm>
using namespace std; const int MAX = ;
struct Node{
int v;
int order;
}node[MAX];
int num[MAX];
int tree[MAX]; int sum(int idx){
int ans = ;
while (idx > ){
ans += tree[idx];
idx -= idx&-idx;
}
return ans;
} void update(int idx, int val)
{
while (idx <= MAX)
{
tree[idx] += val;
idx += idx&-idx;
}
} bool cmp(Node n1, Node n2)
{
return n1.v < n2.v;
} int main()
{
int n;
while (scanf("%d", &n) != EOF && n){
memset(tree, , sizeof(tree));
memset(node, , sizeof(node));
//离散化开始
for (int i = ; i <= n; i++){
scanf("%d", node + i);
node[i].order = i;
}
sort(node + , node + n + , cmp);
for (int i = ; i <= n; i++){
num[node[i].order] = i;
}
//离散化完成,以下为一般的树状数组操作
long long inverseNo = ;
for (int i = ; i <= n; i++){
inverseNo += (i - ) - sum(num[i]);
update(num[i], );
}
printf("%lld\n", inverseNo);
}
return ;
}
参考:
http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html
https://www.topcoder.com/community/data-science/data-science-tutorials/binary-indexed-trees/
poj2299(树状数组+离散化)的更多相关文章
- hdu 4911 Inversion and poj2299 [树状数组+离散化]
题目 题意: 给你一串数字,然后给你最多进行k次交换(只能交换相邻的)问交换后的最小逆序对个数是多少. 给你一个序列,每次只能交换相邻的位置,把他交换成一个递增序列所需要的最少步数 等于 整个序列的 ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学
1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- [HDOJ4325]Flowers(树状数组 离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...
- Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化
1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6321 Solved: 2628[Su ...
- CF 61E 树状数组+离散化 求逆序数加强版 三个数逆序
http://codeforces.com/problemset/problem/61/E 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 会 ...
随机推荐
- 单调队列 + 组合数统计 Gym 101102D
题目链接:http://codeforces.com/gym/101102/problem/D 题目大意:给你一个n*m的矩阵,矩阵里面的数值范围为[1,1e9].这个矩阵有一个值,如果相邻的多个数字 ...
- HDU 6211 卡常数取模 预处理 数论
求所有不超过1e9的 primitive Pythagorean triple中第2大的数取模$2^k$作为下标,对应a[i]数组的和. 先上WIKI:https://en.wikipedia.org ...
- HDU 6061 推导 NTT
复函数,递归代入,可以得到最终的式子为$f(x-\sum_{i=1}^{m}{a_i})$,且$f(x) = \sum_{i = 0}^{n}{c_ix^i}$,求最终各个x项的系数. 设$S=\su ...
- Codeforces Beta Round #34 (Div. 2) E. Collisions
E. Collisions time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- python学习笔记(十六)之文件
打开文件用open函数 open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=Tru ...
- 2017 ACM暑期多校联合训练 - Team 4 1007 HDU 6073 Matching In Multiplication (模拟)
题目链接 Problem Description In the mathematical discipline of graph theory, a bipartite graph is a grap ...
- npm install ERR! code E400/E404
在安装webpack的过程中,出现了一个报错npm install ERR! code E400/E404 解决方法: 1.查看npm配置文件 是否有错误: 执行 npm config edit 查看 ...
- linux 下 genymotion 模拟器不能安装app
提示: "应用未安装" 解决方法: 下载: Genymotion-ARM-Translation_v1.1.zip 进入genymotion 的tools用adb传进去: ./ad ...
- 如何删除git远程分支(转)
1,在开发过程中,大家在远程创建了许多分支,有些是无用的,该如何删除呢,可以参考下面的方法. 如果不再需要某个远程分支了,比如搞定了某个特性并把它合并进了远程的 master 分支(或任何其他存放 稳 ...
- linux系统kill一些类名称相同的进程
jps | grep "Main" | awk '{print $1}' | xargs kill 将其中的 Main 替换为需要kill的进程名即可.