用树状数组求逆序对数(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,其他 ...
随机推荐
- python3的安装,Window与linux
一.window安装 1.首先是window下的安装. 进入python网站https://www.python.org/downloads/下载页面,选择所需要的版本进行下载. 点击Download ...
- python 之re模块(正则表达式) 分组、断言详解
正则表达式分组.断言详解 提示:阅读本文需要有一定的正则表达式基础. 正则表达式中的断言,作为高级应用出现,倒不是因为它有多难,而是概念比较抽象,不容易理解而已,今天就让小菜通俗的讲解一下. 如果 ...
- 我的JavaScript笔记--面向对象
单例模式 ??(基于对象,不能批量生产) var person = { name: "ywb", sayHi: funct ...
- cocos2d-X学习之主要类介绍:场景(CCScene)
场景(CCScene) 类结构: CCScene主要有以下两个函数: bool init () //初始化函数 static CCScene * node (void) //生CCScene 作为 ...
- 【Redis】redis分页查询理解
偶然在代码中发现一个接口,接口定义说是分页查询,但逻辑实现是Redis.不太理解,Redis怎么分页?后来看到一篇文章,然后了解了. 1.Zrevrange实现 通过SortedSet的zrevran ...
- CodeForces 668B Little Artem and Dance
B. Little Artem and Dance time limit per test 2 second memory limit per test 256 megabytes input sta ...
- Spoken English Practice(You know we can't afford that. How do other people do it? Other people make more twenty-four thousand a year. )
绿色:连读: 红色:略读: 蓝色:浊化: 橙色:弱读 下划线_为浊化 口语蜕变(2017/7/9) 英 ...
- php 使用imagettftext()函数出问题的原因
<?php header('Content-type: image/png'); $im = imagecreatetruecolor(400, 300); //创建画布 $white = im ...
- Docker Libnetwork Bridge插件实现代码分析----初始化部分
Bridge driver数据结构如下所示: type driver struct { config *configuration network *bridgeNetwork natChain *i ...
- 列表(List) 的增删改查及其他方法
一.列表的简介 列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[ ]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如:li = ...