Ultra-QuickSort——[归并排序、分治求逆序对]
Description
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
解题思路:
本题揭示了排序的本质——消除逆序对。逆序对,是指对于满足 当i<j时,a[i]>a[j]的序偶(a[i],a[j])。可以证明,交换序列中任意两个
相邻元素,逆序对增加或减少一。那么对于一个给定的序列,按一次交换一对相邻元素的方法,最少需要要交换的次数等于此序列中逆序对的数目。
那么问题就变成了如何求给定序列的逆序对。可以采用分治的方法:
整个序列逆序对数=左半序列逆序对数+右半序列逆序对数+前一个元素在左半序列,后一个元素在右半序列的逆序对数。
再细考虑可以发现,这个过程可以在归并排序的同时完成,时间复杂度为O(NlogN). 注意:可以简单计算一下,n个元素的排列逆序对数最多有 n(n-1)/2个,需要用到long long。 代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
using namespace std;
#define print_time_ printf("time : %f\n",double(clock())/CLOCKS_PER_SEC)
#define maxn 500000
int A[maxn+]; typedef long long LL;
LL DC(int a,int N){
int mid=a+N/-;
int b=a+N-;
if(N==)
return ; LL x1=DC(a, N/);
LL x2=DC(mid+,b-mid);
LL x3=;
int *B=new int[N];
int i=a,j=mid+,p=;
for(;i<=mid&&j<=b&&p<N;p++){
if(A[i]<=A[j]){
B[p]=A[i++];
}
else {
B[p]=A[j++];
x3+=mid-i+;
}
}
while(i<=mid){B[p++]=A[i++];}
while(j<=b){B[p++]=A[j++];}
memcpy(A+a, B, N*sizeof(int));
delete [] B;
return x1+x2+x3;
}
int main() {
int n;
while(scanf("%d",&n)==&&n){
for(int i=;i<n;i++)
scanf("%d",&A[i]);
printf("%lld\n",DC(, n));
}
//print_time_;
return ;
}
Ultra-QuickSort——[归并排序、分治求逆序对]的更多相关文章
- AC日记——codevs 1688 求逆序对
1688 求逆序对 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给定一个序列a1,a2,…, ...
- 【BZOJ4769】超级贞鱼 归并排序求逆序对
[BZOJ4769]超级贞鱼 Description 马达加斯加贞鱼是一种神奇的双脚贞鱼,它们把自己的智慧写在脚上——每只贞鱼的左脚和右脚上各有一个数.有一天,K只贞鱼兴致来潮,排成一列,从左到右第i ...
- poj2299(归并排序求逆序对)
题目链接:https://vjudge.net/problem/POJ-2299 题意:给定一个序列,每次只能交换邻近的两个元素,问要交换多少次才能使序列按升序排列. 思路:本质就是求逆序对.我们用归 ...
- 归并排序+归并排序求逆序对(例题P1908)
归并排序(merge sort) 顾名思义,这是一种排序算法,时间复杂度为O(nlogn),时间复杂度上和快排一样 归并排序是分治思想的应用,我们先将n个数不断地二分,最后得到n个长度为1的区间,显然 ...
- HDU 3743 Frosh Week(归并排序求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 题目意思就是给你一个长为n的序列,让你求逆序对.我用的是归并排序来求的.归并排序有一个合并的过程 ...
- 浙江工商大学15年校赛I题 Inversion 【归并排序求逆序对】
Inversion Time Limit 1s Memory Limit 131072KB Judge Program Standard Ratio(Solve/Submit) 15.00%(3/20 ...
- 2014 HDU多校弟五场A题 【归并排序求逆序对】
这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的 ...
- Day2:T4求逆序对(树状数组+归并排序)
T4: 求逆序对 A[I]为前缀和 推导 (A[J]-A[I])/(J-I)>=M A[j]-A[I]>=M(J-I) A[J]-M*J>=A[I]-M*I 设B[]=A[]-M*( ...
- 归并排序&&归并排序求逆序对
归并排序 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...
随机推荐
- Sublime Text3安装教程,配置教程,常用插件安装等方法
前言: sublimeText3的特点: 1.Sublime Text 是一款跨平台代码编辑器,在Linux.OS X和Windows下均可使用. 2.Sublime Text 是可扩展的,并包含大量 ...
- PHP核心编程-图像操作
一 图像操作环境: 1. 开启GD2图像处理并检测 在php.ini开启GD库 2. 画布坐标系说明 二. 图像基本操作(步骤) 1. 创建图像 创建画布(图像资源) 创建的方法: ...
- PHP学习(类型转化)
PHP 在变量定义中不需要(或不支持)明确的类型定义:变量类型是根据使用该变量的上下文所决定的.也就是说,如果把一个 string 值赋给变量 $var , $var 就成了一个 string .如果 ...
- 转载 初探Promise
初探Promise https://segmentfault.com/a/1190000007032448 javascript es6 promise 33.5k 次阅读 · 读完需要 65 分 ...
- spring-cloud-zuul跨域问题解决
问题发现 正常情况下,跨域是这样的: 1. 微服务配置跨域+zuul不配置=有跨域问题 2. 微服务配置+zuul配置=有跨域问题 3. 微服务不配置+zuul不配置=有跨域问题 4. 微服务不配置+ ...
- 2019-8-31-C#-标准性能测试高级用法
title author date CreateTime categories C# 标准性能测试高级用法 lindexi 2019-08-31 16:55:58 +0800 2018-07-08 0 ...
- python 别名
- React-FlipOver-Counter(日历翻页)
跟窝一起学习鸭~~ //index.js import React from 'react'; import ReactDOM from 'react-dom'; import './index.cs ...
- LeetCode113 Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- 巨蟒python全栈开发-第11阶段 ansible3_1入门四个模块command&shell&script©
大纲 1.系统安装与机器克隆 2.ansible介绍和host-pattern格式 3.command模块 4.shell模块 5.script模块 6.copy模块