http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=232#problem/A

B - Ultra-QuickSort

Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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

题目大意:

给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列。

解题思路:

一看就是冒泡,交换一次记录一次就可以了

但是n的范围达到50W,冒泡O(n^2)的复杂度铁定超时(即使有7000ms,其实这是一个陷阱)

直接用快排又不符合题目的要求(相邻元素交换),快排是建立在二分的基础上的,操作次数肯定比在所要求的规则下的交换次数要更少

那么该怎么处理?

其实这题题目已经给出提示了:Ultra-QuickSort

特殊的快排,能和快排Quicksort相媲美的就是归并排序Mergesort了,O(nlogn)

但是用归并排序并不是为了求交换次数,而是为了求序列的 逆序数(学过《线代》的同学应该很熟悉了)

一个乱序序列的 逆序数 = 在只允许相邻两个元素交换的条件下,得到有序序列的交换次数

例如例子的

9 1 0 5 4

由于要把它排列为上升序列,上升序列的有序就是  后面的元素比前面的元素大

而对于序列9 1 0 5 4

9后面却有4个比9小的元素,因此9的逆序数为4

1后面只有1个比1小的元素0,因此1的逆序数为1

0后面不存在比他小的元素,因此0的逆序数为0

5后面存在1个比他小的元素4, 因此5的逆序数为1

4是序列的最后元素,逆序数为0

因此序列9 1 0 5 4的逆序数 t=4+1+0+1+0 = 6  ,恰恰就是冒泡的交换次数

 

PS:注意保存逆序数的变量t,必须要用龙long long定义,intlong都是无法保存的。。。。会导致WA 

注意__int64类型的输出必须使用指定的c格式输出,printf(“%I64d”,t);

cout是无法输出__int64类型的

 

序列数组s[]int就足够了,每个元素都是小于10E而已

归并排序利用了二分的思想:

划分问题:把序列分成元素个数尽量相等的两半。

递归求解:把两半元素分别排序。

合并问题:把两个有序表合并成一个。

例如输入 1 2 3 4 5  6 7 8

首先分成1 2 3 4||5 6  7 8

之后1 2 |3 4||5 6  7 8

1 2 3 4||5 6 |7 8

1 2 3 4||5  6 7 8

1 2 3 4 5 6  7 8

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
using namespace std;
int a[],n;
long long sum;
void merge(int a[],int l,int mid,int r)
{
int t[];
int i,j,p;
for(i=l,j=mid+,p=;i<=mid&&j<=r;p++)
{
if(a[i]<=a[j]) t[p]=a[i++];
else
{
t[p]=a[j++];
sum=sum+(mid-i+);
}
}
while(i<=mid) t[p++]=a[i++];
while(j<=r) t[p++]=a[j++];
for(int i=l,p=;i<=r;i++)
a[i]=t[p++];
}
void mergesort(int a[],int l,int r)
{
if(l==r) a[l]=a[r];
else
{
int mid=l+(r-l)/;//如果写成mid=(l+r)/2;自我感觉(l+r)会超范围
mergesort(a,l,mid);
mergesort(a,mid+,r);
merge(a,l,mid,r);
}
}
int main()
{
while(scanf("%d",&n)!=EOF&&n!=)
{
sum=;
for(int i=;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,,n-);
printf("%lld\n",sum);
}
return ;
}

Ultra-QuickSort(poj 2299归并排序)的更多相关文章

  1. Ultra-QuickSort - poj 2299 (归并排序+统计逆序数)

    利用归并排序统计逆序数,利用归并求逆序在对子序列s1和s2在归并时(s1,s2已经排好序),若s1[i]>s2[j](逆序状况),则逆序数加上s1.length-i,因为s1中i后面的数字对于s ...

  2. poj 2299 归并排序求逆序数 (可做模板)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 48077   Accepted: 17533 Description In ...

  3. 逆序数 POJ 2299 Ultra-QuickSort

    题目传送门 /* 题意:就是要求冒泡排序的交换次数. 逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序. 一个排列中逆序的总数就称为这个排列的逆 ...

  4. 树状数组求逆序对:POJ 2299、3067

    前几天开始看树状数组了,然后开始找题来刷. 首先是 POJ 2299 Ultra-QuickSort: http://poj.org/problem?id=2299 这题是指给你一个无序序列,只能交换 ...

  5. POJ 2299 Ultra-QuickSort(线段树+离散化)

    题目地址:POJ 2299 这题以前用归并排序做过.线段树加上离散化也能够做.一般线段树的话会超时. 这题的数字最大到10^10次方,显然太大,可是能够利用下标,下标总共仅仅有50w.能够从数字大的開 ...

  6. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  7. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  8. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  9. POJ 2299 Ultra-QuickSort 归并排序、二叉排序树,求逆序数

    题目链接: http://poj.org/problem?id=2299 题意就是求冒泡排序的交换次数,显然直接冒泡会超时,所以需要高效的方法求逆序数. 利用归并排序求解,内存和耗时都比较少, 但是有 ...

随机推荐

  1. vim 正则替换【转】

    :[range]s/from/to/[flags] range:搜索范围,如果没有指定范围,则作用于但前行. :1,10s/from/to/ 表示在第1到第10行(包含第1,第10行)之间搜索替换: ...

  2. CDH localhost:7180 页面无法打开

    有时会遇到CDH集群的7180页面无法访问,通过命令查看服务发现: service --status-all cloudera-scm-server dead but pid file exists ...

  3. SPClaimsUtility.AuthenticateFormsUser的证书验证问题

    Log Parser Studio查看IIS日志发现调用SPClaimsUtility.AuthenticateFormsUser的部分有time-taken在15秒左右的多个响应,查看call st ...

  4. ElasticSearch 简单入门

    英文原文:Getting Started with ElasticSearch 原文链接:http://www.oschina.net/translate/elasticsearch-getting- ...

  5. html如何让label在div中的垂直方向居中显示?

    设置label的行高 line-height 和div的高度一致即可.

  6. iOS - 开发代码部分规范

    1. 关于命名 1.1 统一要求 含义清楚,尽量做到不需要注释也能了解其作用,若做不到,就加注释 使用全称,不适用缩写 1.2 类的命名 大驼峰式命名:每个单词的首字母都采用大写字母 例子:MFHom ...

  7. 访问php文件显示源码

    前天新装了个LAMP的环境,兴冲冲的clone下来代码,结果一访问乐子就大了,直接显现源码 面对这个问题,冥思苦想,四处找资料啊 让我改这改那的,最后终于找到症结 Ubuntu 16.04 系统 LA ...

  8. 如何写好PPT

    怎样写好ppt? 阿里巴巴矢量图标库 优品PPT SmartArt PPT美化大师

  9. SQL Fundamentals: Using Single-Row Functions to Customize Output使用单行函数自定义输出

    SQL Fundamentals || Oracle SQL语言 DUAL is a public table that you can use to view results from functi ...

  10. 【紫书】 Unix ls UVA - 400 模拟

    题意:中文版https://vjudge.net/problem/UVA-400#author=Zsc1615925460 题解:首先读取字符,维护一个最长字符串长度M,再排序. 对于输出,写一个pr ...