POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化
我用的线段树写的。
num数组表示已插入的数值的个数。
由于a[i]数值很大,但是n不是很大,所以要离散化处理
9 1 0 5 4
离散化后
4 1 0 3 2
这样保证最大值不会超过n
#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std; typedef long long ll;
const int MAXN = 543210;
int a[MAXN], mp[MAXN], num[MAXN<<2]; void push_up(int rt)
{
num[rt] = num[rt<<1] + num[rt<<1|1];
} void update(int p, int l, int r, int rt)
{
if(l == r)
{
num[rt]++;
return;
}
int m = (l + r) >> 1;
if(p <= m) update(p, lson);
else update(p, rson);
push_up(rt);
} int query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R) return num[rt];
int m = (l + r) >> 1;
int ret = 0;
if(L <= m) ret += query(L, R, lson);
if(R > m) ret += query(L, R, rson);
return ret;
} bool cmp(int A, int B)
{
return a[A] < a[B];
} int main()
{
// freopen("in.txt", "r" ,stdin);
int n;
while(~scanf("%d", &n) && n)
{
for(int i=0; i<n; i++)
{
scanf("%d", &a[i]);
mp[i] = i;
}
//离散化,排序后处理
sort(mp, mp+n, cmp); //离散化排序
for(int i=0; i<n; i++)
a[mp[i]] = i; //离散化处理
ll ans = 0;
memset(num, 0, sizeof(num));
for(int i=0; i<n; i++)
{
ans += query(a[i], n-1, 0, n-1, 1);
update(a[i], 0, n-1, 1);
}
printf("%I64d\n", ans);
}
return 0;
}
POJ 2299 Ultra-QuickSort 求逆序数 线段树或树状数组 离散化的更多相关文章
- poj 2299 Ultra-QuickSort (归并排序 求逆序数)
题目:http://poj.org/problem?id=2299 这个题目实际就是求逆序数,注意 long long 上白书上的模板 #include <iostream> #inclu ...
- poj 2299 Ultra-QuickSort 归并排序求逆序数对
题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题 ...
- POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 70674 Accepted: 26538 ...
- Poj 2299 Ultra-QuickSort(归并排序求逆序数)
一.题意 给定数组,求交换几次相邻元素能是数组有序. 二.题解 刚开始以为是水题,心想这不就是简单的冒泡排序么.但是毫无疑问地超时了,因为题目中n<500000,而冒泡排序总的平均时间复杂度为, ...
- poj 2299 Ultra-QuickSort(求逆序对)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 52778 Accepted: 19348 ...
- POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 39279 Accepted: 14163 ...
- 求逆序数的方法--线段树法&归并排序法
逆序数的概念:对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,就说有1个逆 ...
- poj 3067 Japan(树状数组求逆序数)
链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
随机推荐
- 在Linux中通过Top运行进程查找最高内存和CPU使用率
按内存使用情况查找前15个进程,在批处理模式下为"top" 使用top命令查看有关当前状态,系统使用情况的更详细信息:正常运行时间,负载平均值和进程总数. 分类:Linux命令操作 ...
- 本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法。
本文介绍使用windows系统自带的远程桌面mstsc连接Centos 7.x远程桌面的基本方法. 一.前言 我希望用windows远程访问centos图形界面.xmanager连接centos远程桌 ...
- Bash shell的特性
bash的特性 命令补全 Tab键 命令补全 路径补全 选项补全 yum install -y bash-completion -长格式 --all -短格式 -a 快 ...
- bond0 配置ip不生效排查方法
今天巡检的时候,发现无法连接到服务器上面了,于是到机房连接到显示器查看: 1.先检查网卡,协议有没有问题. 2.远程链接的网卡名称是bond0,用ifconfig看看网卡配置,然后发现配置的 ...
- Linux中级之lvs三个模式的图像补充(nat,dr,tun)
负载均衡(Load Balance)集群提供了一种廉价.有效.透明的方法,来扩展网络设备和服务器的负载.带宽.增加吞吐量.加强网络数据处理能力.提高网络的灵活性和可用性. (1)单台计算机无法承受大规 ...
- GPU加速:宽深度推理
GPU加速:宽深度推理 Accelerating Wide & Deep Recommender Inference on GPUs 推荐系统推动了许多最流行的在线平台的参与.随着为这些系统提 ...
- 孟老板 BaseAdapter封装 (二) Healer,footer
BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...
- 绘制log()函数图像,并在图上标注选定的两个点
绘制log()函数图像,并在图上标注选定的两个点 import math import matplotlib.pyplot as plt if __name__ == '__main__': x = ...
- 一文彻底理解Apache Hudi的多版本清理服务
Apache Hudi提供了MVCC并发模型,保证写入端和读取端之间快照级别隔离.在本篇博客中我们将介绍如何配置来管理多个文件版本,此外还将讨论用户可使用的清理机制,以了解如何维护所需数量的旧文件版本 ...
- 【SQLite】SQLite文件突然变大怎么办?瘦身办法
使用VACUUM命令即可: VACUUM 命令通过复制主数据库中的内容到一个临时数据库文件,然后清空主数据库,并从副本中重新载入原始的数据库文件.这消除了空闲页,把表中的数据排列为连续的,另外会清理数 ...