POJ 2299 Ultra-QuickSort(逆序数)

【程序设计大赛刷题记录】C语言 05 【Ultra-QuickSort】 链接:https://ac.nowcoder.com/acm/contest/1001/E 来源:牛客网 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 seque 阅读详情
                                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 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
题意:给定一个数组,每次可以交换相邻两个数组的值,求最少交换次数使数组变为递增数组。
题解:标准的逆序数模板。

AC代码:

#include <iostream>
using namespace std;
const int maxn = 500006;
int n,a[maxn],b[maxn];
long long sum;
void merge(int l,int mid,int r)
{
	int i = l;
	int j = mid+1;
	for(int k=l;k<=r;k++)
	{
		if(j>r||i<=mid&&a[i]<a[j])b[k]=a[i++];
		else 
		{
			b[k]=a[j++];
			sum+=mid-i+1;
		}
	}
	for(int k=l;k<=r;k++)a[k]=b[k]; 
}
void update_merge(int start,int end)
{
	if(start>=end)return ;
	int mid = (start+end)/2;
	update_merge(start,mid);
	update_merge(mid+1,end);
	merge(start,mid,end);
}
int main(int argc, char const *argv[])
{
	while(cin>>n)
	{
		if(!n)break;
		sum=0;
		for(int i=1;i<=n;i++)cin>>a[i];
		update_merge(1,n);
		cout<<sum<<endl;
	}

	return 0;
}

Claude Code长期记忆:优雅的上下文管理 ClaudeCode的上下文管理系统采用三层架构设计,通过智能压缩和优化策略在记忆容量与性能间取得平衡。系统包含短期记忆(高速工作区)、中期记忆(智能蒸馏器)和长期记忆(持久化知识库)。当Token使用率达到92%阈值时启动wU2压缩器,采用8段式结构化摘要保留关键信息,并配合智能文件恢复和渐进式预警机制。该系统通过分层处理、优雅降级和透明化交互,为AI应用提供了高效可靠的上下文管理方案,展现了在有限资源下实现智能记忆的艺术。 阅读详情

相关推荐

FPGA新手必看:XC7Z020最小系统搭建全攻略(附原理图解析)

本文为FPGA新手提供了XC7Z020最小系统搭建的完整实战指南。文章详细解析了电源架构、时钟复位、配置调试等关键硬件电路设计,并重点剖析了外围电路与端口引脚的连接要点及常见陷阱,帮助读者从零开始构建稳定可靠的开发平台。

weixin_29189377的博客 359

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 sorted in ascending order. For the input sequence 9 1 0 5 4 ,

Lionel 3052

ArcGIS栅格拼接全攻略:从核心原理到色彩平衡与大数据处理

在GIS数据处理中,栅格拼接是一项基础且关键的操作,其本质是将多幅具有重叠区域的栅格图像无缝整合为一张完整地图。该技术涉及空间参考统一、像元对齐与色彩融合等核心原理,是构建连续地理空间数据的基石。其技术价值在于保障数据完整性、提升可视化效果,并为后续的空间分析提供可靠底图。在遥感影像处理、数字高程模型构建及地图制图等应用场景中,高质量的拼接成果至关重要。本文聚焦于ArcGIS平台,深入解析其栅格拼接工具,并针对常见的色彩不一致与大数据量处理难题,提供了基于**色彩平衡**和**金字塔**优化的实战解决方案,

weixin_33158887的博客 262

poj2299树状数组入门

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

qq_43813140的博客 462

Python3.8下MvCameraControl.dll加载失败?3种方法彻底解决FileNotFoundError

本文针对Python3.8及以上版本在Windows平台调用MvCameraControl.dll时出现的FileNotFoundError问题,深入剖析了其根源在于Python 3.8引入的DLL加载安全机制变更。文章提供了三种核心解决方案:显式指定DLL完整路径、使用os.add_dll_directory()添加可信目录,以及利用ctypes的winmode参数进行精细控制,并给出了工程化封装建议,帮助开发者彻底解决此兼容性问题。

weixin_29093017的博客 191

HT7533与1117稳压芯片动态特性对比:实测分析与优化策略

本文深入对比了HT7533与1117稳压芯片的动态特性,包括电压过冲和瞬态响应能力,并提供了实测数据与优化策略。HT7533适合低功耗设备,而1117在大电流和稳定性要求高的场景表现更优。文章还分享了PCB布局和电容选择的实用建议,帮助工程师根据项目需求选择合适的稳压芯片。

weixin_28454475的博客 610

[2014.3.29]poj2299 Ultra-QuickSort 解题报告(C++)

【原题】 [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 t

拿着板子的狼犬 756

大模型越狱指令数据集全解析:从AdvBench到HarmBench

本文全面解析了AdvBench、HarmBench等主流大模型越狱指令数据集。AdvBench作为基础测试集,用于快速评估模型安全基线;HarmBench则以其全面的文本、多模态及对抗性攻击评估,成为当前权威的深度安全审计工具。文章还介绍了MaliciousInstruct、JailbreakBench等特色数据集,并提供了根据实际场景选择与使用这些数据集的具体实战指南。

weixin_29327525的博客 618

归并排序

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 , Ul

STDEN的博客 415

海德汉机床采集数据

海德汉数控系统我一般在高端机床上见得比较多。有好多五轴加工精度比较高的机床都用的是海德汉的系统。市面上常见的有530和640等型号。海德汉的采集还是有点难度的,需要对SDK有深度的了解。

若云流风的专栏 1528

智能座舱SoC平台横评:SA8155P、SA8255P、SA8295P、MT8678、MT8628 谁是下代车机首选?

高通SA8295P以PC级架构、强悍AI算力,代表安卓阵营迈向“本地大模型+空间计算”阶段;联发科MT8678则凭借低功耗 + 高带宽,成为副驾多媒体/副屏智能化的性价比之王。在座舱设计中,SoC平台决定了你能给用户什么样的交互方式,选型时切忌只看跑分,而要回归“体验-热设计-平台生态”的三角平衡。

weixin_43199439的博客 3358

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的博客 304

Matlab在线性代数中的应用(一):向量组的线性相关性

求列向量组A的一个最大线性无关组可用命令rref(A)将A化成行最简形,其中单位向量对应的列向量即为最大线性无关组所含向量,其它列向量的坐标即为其对应向量用最大线性无关组线性表示的系数。 01 — 例1 求下列矩阵列向量组的一个最大无关组。 解:编写脚本文件如下: >>format rat a=[1,-2,-1,0,2;-2,4,2,6,-6;2,-1,0,2,3;3,3,3,3,4]; b=rref(a) 求得: b...

wenyusuran的专栏 3237

POJ 2299 Ultra-QuickSort(求逆序数)

题目链接: http://poj.org/problem?id=2299 解题思路: 树状数组实际上就是一个数组,只不过它的每个元素保存了跟原来数组的一些元素相关的结合值。 若A为原数组,定义数组C为树状数组。C数组中元素C[ i ]表示A[ i –lowbit( i ) + 1]至A[ i ]的结合值。 lowbit(i)是i的二进制中最后一个不为零的位数的2次方,可以这样

piaocoder 423

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

逆序数poj 2299 Ultra-QuickSort sgu 180 Inversions

poj 2299

晔嫁的专栏 734

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

【原】 POJ 2299 Ultra-QuickSort 逆序数 解题报告

http://poj.org/problem?id=2299 方法: 利用merge sort求逆序数,复杂度nlgn 如果比较每一对儿数(或使用bubble sort),复杂度为n^2,太慢 对于一对儿逆序对儿,有三种情况:两数都在数组左半边,两数都在数组右半边,两数分别在左右两个半边。 由于Merge时两个半边都为sorted,所以只会出现第三种情况。...

ajiaxi111111的博客 170

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

逆序数 poj2299 Ultra-QuickSort

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

zxy160的博客 646

归并排序求逆序数【Ultra-QuickSort POJ - 2299 】

在这里插入代码片`void Mergesort(int x,int y) { if(y>x) { int m=x+(y-x)/2; int p=x,q=m+1,i=x; Mergesort(x,m); Mergesort(m+1,y); while(p<=m||q<=y) { if(q>y||a[p]<=a[q]&&p&l...

谁是凶手1703 214

poj2299Ultra-QuickSort【逆序数】

Language: Default Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 50448   Accepted: 18500 Description In this problem, you have to analyz

走一步再走一步 578

POJ 2299 Ultra-QuickSort(逆序数、分治)

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

Algobird 563

POJ 2299--Ultra-QuickSort【逆序数】

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

_阿阿阿阿欢 411

poj 2299 Ultra-QuickSort 树状数组求逆序数

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

kimnoic的专栏 326

Poj 2299 Ultra-QuickSort (归排求逆序数)

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

Grit_ICPC的博客 636

POJ 2299 Ultra-QuickSort (归并排序求逆序数)

POJ 2299 Ultra-QuickSort (归并排序求逆序数)

Tc的专栏 2459

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

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

The_star_is_at的博客 639

poj-2299 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 seque

心の コード 1247
上一篇: 归并排序模板
下一篇: bzoj 1271 秦腾与教学评估 (防线)
sortmin
博客等级 码龄9年 45粉丝 178原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值