用树状数组求逆序对数(poj2299)
| Time Limit: 7000MS | Memory Limit: 65536K | |
| Total Submissions: 46995 | Accepted: 17168 |
Description
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
Source
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
int N,M;
struct node{
int val;//记录大小
int pos;//记录这个数字的位置,方便以后排序
};
node a[];//暂时存储序列
int reflect[];//离散化后的序列
int BIT[];//树状数组 int cmp(const node &u,const node &v){
if(u.val<v.val) return ;
return ;
} int lowbit(int x){
return x&(-x);
}
/*
update
把数字依次插入,而不是直接建树的原因:
我们目的是求之前比当前数字小的数字个数,这样可以做到
ans+=i-sum(reflect[i])来更新,直接建树。。。不行
*/ void update(int x){
for(int i=x;i<=N;i+=lowbit(i)){
BIT[i]++;
}
} int sum(int k){
int ANS=;
for(int i=k;i>;i-=lowbit(i)){
ANS+=BIT[i];
}
return ANS;
}
int main(){
while(scanf("%d",&N)!=EOF&&N){
for(int i=;i<=N;i++){
scanf("%d",&a[i].val);
a[i].pos=i;
}
sort(a+,a+N+,cmp); for(int i=;i<=N;i++)
reflect[a[i].pos]=i;//离散化
for (int i = ; i <= N; ++i) BIT[i] = ;
//清空树状数组,,,千万不要忘记
LL ans=;
for(int i=;i<=N;i++){
update(reflect[i]);
ans+=(i-sum(reflect[i]));
}
printf("%lld\n",ans);
} return ;
用树状数组求逆序对数(poj2299)的更多相关文章
- poj 2299 树状数组求逆序对数+离散化
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 54883 Accepted: 20184 ...
- 用归并排序或树状数组求逆序对数量 poj2299
题目链接:https://vjudge.net/problem/POJ-2299 推荐讲解树状数组的博客:https://blog.csdn.net/int64ago/article/details/ ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)
[NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...
- 【bzoj2789】[Poi2012]Letters 树状数组求逆序对
题目描述 给出两个长度相同且由大写英文字母组成的字符串A.B,保证A和B中每种字母出现的次数相同. 现在每次可以交换A中相邻两个字符,求最少需要交换多少次可以使得A变成B. 输入 第一行一个正整数n ...
- “浪潮杯”第九届山东省ACM大学生程序设计竞赛(重现赛)E.sequence(树状数组求逆序对(划掉))
传送门 E.sequence •题意 定义序列 p 中的 "good",只要 i 之前存在 pj < pi,那么,pi就是 "good": 求删除一个数, ...
- [NOI导刊2010提高&洛谷P1774]最接近神的人 题解(树状数组求逆序对)
[NOI导刊2010提高&洛谷P1774]最接近神的人 Description 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某 ...
- 2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对)
2021.12.10 P5041 [HAOI2009]求回文串(树状数组求逆序对) https://www.luogu.com.cn/problem/P5041 题意: 给一个字符串 \(S\) ,每 ...
- NOIP 2013 洛谷P1966 火柴排队 (树状数组求逆序对)
对于a[],b[]两个数组,我们应选取其中一个为基准,再运用树状数组求逆序对的方法就行了. 大佬博客:https://www.cnblogs.com/luckyblock/p/11482130.htm ...
- 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)
链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...
随机推荐
- TypeScript 函数 (五)
传递给一个函数的参数个数必须与函数期望的参数个数一致. 参数类别: 必须参数 可选参数 :可选参数必须在参数后面. 默认参数 :当用户没有传递这个参数或传递的值是undefined时. 它们叫做有默认 ...
- 关于 HTML5 的文件上传处理,兼容性问题,以及 BLOB 对象的使用 (转载)
研究过程中关于本主体的相关参考 好文:https://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/ 好文:http: ...
- Error in registration. Error: Error Domain=NSCocoaErrorDomain Code=3000 "未找到应用程序的“aps-environment”的授
本文转载至 http://blog.csdn.net/woaifen3344/article/details/41311023 Code3000极光推送erroryour certificate n ...
- Linux下自动调整时间和时区与Internet时间同步
(原文链接) 调整linux系统时间和时区与Internet时间同步一.修改时区:# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime修改为中国的 ...
- STL中的容器
STL中的容器 一. 种类: 标准STL序列容器:vector.string.deque和list. 标准STL关联容器:set.multiset.map和multimap. 非标准序列容器slist ...
- Decorate设计模式
定义:装饰器设计模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案.不改变对象的情况下,动态增加其功能. • 需要扩展一个类的功能,或给一个类添加附加的成员. • 需要动态地给一个对象增 ...
- Es 中一个分片一般设置多大
百度Elasticsearch-产品描述-介绍-百度云 https://cloud.baidu.com/doc/BES/FAQ.html#.2C.BB.93.08.C9.7E.2F.A3.E7.35. ...
- 小团队交流为什么 :wq! :wq 二者结果一致?
w 答案: :q 执行失败--->提示-已经修改,但是尚未保存,+!强制不保存退出 :w 保存
- telnet --- no route to host solution "iptables -F " in the target machine
telnet --- no route to host solution "iptables -F " in the target machine
- Spring Data 关于Repository的介绍(四)
Repository类的定义: public interface Repository<T, ID extends Serializable> { } 1)Repository是一个空接口 ...