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 ...
随机推荐
- 【转载】复制文件到已存在的Jar
复制文件到已存在的Jar 技术标签: Ant OSGI Eclipse 脚本 配置管理 问题 这两天在写一个小东西.这个小东西是一个大东西的一部分.其实也就是其中的一两个类.而这个大东西需 ...
- window 共享打印机
https://www.zhihu.com/question/20653708 https://h30471.www3.hp.com/t5/da-yin-ji-yu-sao-miao-yi-de-an ...
- zabbix官方源替换为阿里云的zabbix源,一键脚本。(安装zabbix报错curl#18 - "transfer closed with 2988713 bytes remaining to read":15 ETA Trying other mirro)
最近突然安装zabbix总是报错,比如 (24/27): t1lib-5.1.2-14.el7.x86_64.rpm | 166 kB 00:00:00 zabbix-web-4.4.6-1.el7. ...
- 026.Python面向对象类的相关操作以及对象和类的删除操作
类的相关操作 定义的类访问共有成员的成员和方法 定义的类动态添加公有成员的属性和方法 定义的类删除公有成员的属性和方法 1 定义一个基本的类 #定义一个类 class Plane(): #添加一个共有 ...
- 022.Python模块序列化模块(json,pickle)和math模块
序列化模块 一 序列化模块 pickle 1.1 基本认识 序列化:把不能够直接存储的数据变成可存储的过程就是序列化 反序列化:把储存的数据拿出来恢复成原来的数据类型就是反序列化 例如,一个文件不可 ...
- linux 服务开机自启动systemd方式 (Centos7)
linux 服务开机自启动systemd方式 (Centos7) 1.编写一个 /opt/hello.sh 脚本 [root@jws-ftp prometheus]# cat /opt/hello.s ...
- Day029 JDK8中新日期和时间API (四)
JDK8中新日期和时间API 其他的一些API ZoneId:该类中包含了所有的时区信息,一个时区的ID,如 Europe/Paris ZonedDateTime:一个在ISO-8601日历系统时区的 ...
- 用 Python 写个贪吃蛇,保姆级教程!
本文基于 Windows 环境开发,适合 Python 新手 本文作者:HelloGitHub-Anthony HelloGitHub 推出的<讲解开源项目>系列,本期介绍 Python ...
- 『动善时』JMeter基础 — 34、JMeter接口关联【XPath提取器】
目录 1.XPath提取器介绍 2.XPath提取器界面详解 3.XPath提取器的使用 (1)测试计划内包含的元件 (2)网易首页请求界面内容 (3)XPath提取器界面内容 (4)百度首页请求界面 ...
- 机器学习算法之K近邻算法
0x00 概述 K近邻算法是机器学习中非常重要的分类算法.可利用K近邻基于不同的特征提取方式来检测异常操作,比如使用K近邻检测Rootkit,使用K近邻检测webshell等. 0x01 原理 ...