poj2299 Ultra-QuickSort 树状数组 + 离散化

伪距单点定位算法原理以及利用最小二乘法计算实例 即该公式:建立下列代价函数满足最佳估值的条件为:即:可解得未知参数 X 的最佳估值由于每个可见卫星观测量的精度可能不一样,这时,需要对不同观测量赋予一个权重系数,称为加权。 阅读详情

Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 56422 Accepted: 20845

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

The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.

Output

For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.

Sample Input

5
9
1
0
5
4
3
1
2
3
0

Sample Output

6
0


a[i]的数据范围太大,直接存树状数组存不开,要离散化一下。因为题目中说明了a中每个数字是唯一的,所以可以对a中的数重新赋一个新的较小值,并且不改变他们的相对大小,这样数据范围就可以减小到n了,再用树状数组求逆序数就行了


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cstdlib>

using namespace std;

typedef long long ll;

struct node {
	int num, i;
	node() {}
	node(int i, int num) : i(i), num(num) {}
	bool operator < (const node& on) const {
		return this->num < on.num;
	}
}s[500005];

int n, cnt;
int a[500005], BIT[500005];

int lowbit(int x) {
	return x & (-x);
}

void add(int x, int y) {
	for (int i = x; i <= cnt; i += lowbit(i)) {
		BIT[i] += y;
	}
}

int sum(int x) {
	int res = 0;
	for (int i = x; i > 0; i -= lowbit(i)) {
		res += BIT[i];
	}
	return res;
}

int Sum(int l, int r) {
	return sum(r) - sum(l - 1);
}

int main()
{
	while (~scanf("%d", &n) && n) {
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			s[i] = node(i, a[i]);
		}
		sort(s + 1, s + 1 + n);
		cnt = 0;
		for (int i = 1; i <= n; i++) {
			a[s[i].i] = ++cnt;
		}
		ll ans = 0;
		memset(BIT, 0, sizeof(BIT));
		for (int i = 1; i <= n; i++) {
			add(a[i], 1);
			ans += Sum(a[i] + 1, cnt);
		}
		printf("%lld\n", ans);
	}
	return 0;
}


用Python玩转ROS机械臂:YOLO目标检测+MoveIt!自动抓取仿真教程 本文详细介绍了如何利用Python集成YOLO目标检测与ROS的MoveIt!运动规划库,实现机械臂的智能抓取仿真。通过Gazebo仿真环境,从视觉感知、坐标转换到运动规划与执行,构建了一套完整的自动化抓取系统,为机器人视觉抓取应用提供了可复现的实践方案。 阅读详情

相关推荐

算法导论之线性规划

线性规划,充斥着运筹学,在图的单源最短路径求解差分约束系统就是用到线性规划。怎么样问题可以建模为线性规划来解决呢?在给定的有限的资源和竞争约束情况下,取得最大化或最小化目标的问题。导论中给出政治竞选问题、航空航线调度问题、钻井采油问题。最大化或最小化目标是函数的因变量,自变量就是资源的约束因素,其函数就是由这些制约因素构成的等式或不等式。下面数学定义下线性规划。 在一般线性规划问题中,最优化一个

医疗影像检索 2万+

POJ 2299 Ultra-QuickSort(逆序数 树状数组+离散化)

- POJ 2299 - Ultra-QuickSort Time Limit: 7000MS | Memory Limit: 65536K 题意: 給定一个长度为 n 的数组,用冒泡排序使得它有序,求交换操作的次数;即求该数组的逆序数之和或者逆序对个数。(有多组输入,当 n 为 0 时输入结束) > 逆序数 数据范围: n < 500000,0 ≤ a[i] ≤ 9999999...

Pandora_heart的博客 306

使用petalinux做嵌入式设计开发(1)

1.创建vivado工程,建立硬件环境,最终导出hdf文件 既可以在windows系统下的vivado上编写硬件工程并导出hdf文件,也可以在linux系统下的vivado中编写硬件工程并导出hdf文件。(对于windows系统下导出的hdf文件还需要传递到linux系统下的文件夹中。) 2.以hdf硬件文件为基础创建,配置,编译,打包petalinux工程,导出linux系统所需镜像文件。 具体步骤: (0)设置环境变量 设置环境变量使得我们可以直接使用petalin...

plotup的博客 2085

POJ 2299 Ultra-QuickSort 【树状数组+离散化】

传送门:POJ 2299 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Problem Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence o...

Waiting For You 321

【雷达信号处理实战】从原理到代码:脉冲多普勒(PD)处理全流程解析与MATLAB仿真

本文深入解析了脉冲多普勒(PD)雷达信号处理的核心原理与全流程。通过剖析多普勒效应、脉冲压缩与慢时间FFT等关键技术,并结合详细的MATLAB仿真代码,手把手指导如何从信号建模、回波模拟到最终生成距离-多普勒谱,实现运动目标的精确测距与测速,直观展示了相参积累带来的显著性能增益。

pca5navigator的博客 980

STM32F407编译下载全流程:从CubeMX到HEX烧录

嵌入式固件开发中,编译与下载是连接代码与硬件的关键闭环。其本质是将高级语言描述的逻辑,经交叉编译(ARM GCC)、链接(ELF生成)、格式转换(HEX/BIN)后,通过调试接口(SWD/UART)写入MCU Flash存储器的过程。该流程涉及工具链协同、二进制格式特性(如Intel HEX的地址映射与无元数据设计)、启动模式(System Memory Bootloader)、Flash选项字节(RDP/WRP)等底层机制,直接决定固件可运行性与安全性。典型应用场景包括STM32F407开发板调试、量产固

weixin_28840811的博客 275

POJ2299 Ultra-QuickSort 树状数组+离散化

题目链接:http://poj.org/problem?id=2299 题目大意: 在某些问题上,你必须分析特定的排序算法。 该算法处理一系列n个不同的整数通过交换两个相邻序列元素,直到按升序排序序列。 对输入序列  9 1 0 5 4, 超快速排序生成的输出  0 1 4 5 9。 你的任务是确定超快速排序需要执行多少互换操作对于给定输入序列。 输入: 测

AC_Gibson的专栏 613

别再只玩积木了!用乐高EV3和官方软件,手把手教你做个能巡线的小车(附完整代码)

本文详细介绍了如何利用乐高EV3套件和官方软件制作智能巡线机器人,从硬件组装到软件编程全面覆盖。通过传感器原理讲解、控制算法调试等工程化细节,帮助读者掌握机器人开发的核心理念,特别适合STEAM教育和硬件编程爱好者实践。

weixin_30776273的博客 461

POJ2299 Ultra-QuickSort(树状数组+离散化)

POJ2299 Ultra-QuickSort(树状数组+离散化)

Pure_vv的博客 306

CATIA二次开发一一一计算 CATIA 零件模型在全局坐标系下的包围盒方法

本文介绍了一种在CATIA中计算零件模型全局坐标系下包围盒(BoundingBox)的方法。通过遍历零件几何体特征点,并应用坐标变换矩阵,最终返回精确的零件包围盒。核心步骤包括初始化与输入校验、坐标变换处理、几何体特征点遍历以及全局坐标变换。关键方法涉及CATMathBox操作和坐标变换接口。该方法适用于碰撞检测、可视化优化和装配分析等场景,能够快速判断零件间的空间干涉,提升渲染性能,并计算零件在装配体中的全局位置范围。

工业软件开发 908

ROSTCM6+Gephi的网络语义分析详细教程(附案例实战)

ROSTCM6是武汉大学沈阳教授研发编码的国内目前唯一的以辅助人文社会科学研究的大型免费社会计算平台。该软件可以实现微博分析、聊天分析、全网分析、网站分析、浏览分析、分词、词频统计、英文词频统计、流量分析、聚类分析等一系列文本分析。Gephi是一款开源免费跨平台基于JVM的复杂网络分析软件,可用于各种图形和网络的可视化和探索,是最受欢迎的网络可视化软件之一。

m0_64336780的博客 1万+

POJ2299-Ultra-QuickSort(树状数组+离散化)

POJ2299 原题链接:http://poj.org/problem?id=2299 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 63588   Accepted: 23705 Description In thi

Yorozuya 555

微机接口 上机考试题目及代码

吉林大学微机接口 上机考试 A , B 卷内容及代码

一杯水果茶!足矣~ 2008

POJ Ultra-2299 QuickSort 【离散化+树状数组】

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 55716   Accepted: 20578 Description In this problem, you have to analyze a particular sorti

博客已搬迁到http://www.cnblogs.com/cniwoq/ 337

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【树状数组+离散化】【逆序对】 POJ 2299 Ultra-QuickSort

题目链接 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 so...

FFOYeah的博客 390

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

poj 2299 Ultra QuickSort 树状数组+离散化

poj 2299 Ultra QuickSor 树状数组+离散化

klc的博客 465

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

POJ2299Ultra-QuickSort(树状数组+离散化)

题目链接:POJ2299 题目大意:给你n个数字,问这段序列类似于冒泡排序后至少需要几次交换。 解题思路:实际上就是求数列的逆序数,树状数组可以解决,由于数据规模大到达long long的规模,离散化一下。注意开的val要为long long! AC代码: #include &amp;lt;cstdio&amp;gt; #include &amp;lt;cstring&amp;gt; #include &amp;lt;cstdlib&amp;.

小胡同的诗 215

POJ 2299 Ultra-QuickSort (离散化+树状数组)

树状数组一个优美的数据结构

n-1 768

POJ2299-Ultra-QuickSort(树状数组 离散化)

题目链接 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 s...

QAQ 195

Ultra-QuickSort poj2299(树状数组+离散化)

Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 80376 Accepted: 30098 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proces...

weixin_43736492的博客 276

树状数组 求逆序数 poj 2299 离散化

树状数组 求逆序数 poj 2299 这里说的很好,把求逆序的步骤说的很明白,我也是看完才懂的,之前自己想了很久就是不明白为什么可以用树状数组求逆序    转载: 树状数组,具体的说是 离散化+树状数组。这也是学习树状数组的第一题. 算法的大体流程就是: 1.先对输入的数组离散化,使得各个元素比较接近,而不是离散的, 2.接着,运用树状数组的标准操作来累计数组的逆序数。 算法详

The_star_is_at的博客 641

【Poj】-2299-Ultra-QuickSort(树状数组,离散化, 好)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 55721   Accepted: 20582 Description In this problem, you have to analyze a particular sorti

Bear1998的博客 479

POJ-2299 Ultra-QuickSort (树状数组,离散化,C++)

Problem 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 unti...

awang50683的博客 197

POJ-2299_Ultra-QuickSort (离散化+树状数组)

点击这里即可提交代码 Ultra-QuickSort Time Limit:7000MS Memory Limit:65536K Total Submissions:78254 Accepted:29347 Description In this problem, you have to analyze a particular s...

From now on...的Blogs 232

树状数组 离散化 求逆序数POJ 2299Ultra-QuickSort解题报告

树状数组 离散化 求逆序数POJ 2299Ultra-QuickSort解题报告

Dafang_Xu的博客 834

POJ 2299 Ultra-QuickSort(归并排序,树状数组,离散化)

http://poj.org/problem?id=2299 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 55702   Accepted: 20570 Description In this problem

Shyazhut 365
上一篇: LA4329 Ping pong 树状数组前缀和
下一篇: hdu5000 Clone dp
Daemoonn
博客等级 码龄11年 42粉丝 198原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址