Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 48111 | Accepted: 17549 |
Description

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 i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0 题意就是求所给序列的逆序数
可以用归并排序求#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define WW freopen("output.txt","w",stdout)
#define RR freopen("input.txt","r",stdin) using namespace std; const int MAX= 500100 ; int a[MAX];
int b[MAX];
int n;
LL sum;//开始估计错值,用int 直接爆掉了
void Link_Sqrt(int low,int mid,int Low,int high)
{
int i=low,j=Low,s=0;
while(i<=mid&&j<=high)
{
while(a[i]<=a[j]&&i<=mid&&j<=high)
{
b[s++]=a[i++];
}
while(a[j]<a[i]&&i<=mid&&j<=high)//当a[i]>a[j]时,因为a[i--mid]与a[mid=1--high]都是有序序列,所以对与a[j]他的逆序数就是mid-i+1
{
sum+=(mid-i+1);
b[s++]=a[j++];
}
}
while(i<=mid)
{
b[s++]=a[i++];
}
while(j<=high)
{
b[s++]=a[j++];
}
for(int k=0;k<s;k++)
{
a[low+k]=b[k];
}
}
void Sqrt(int low,int high)
{
int i=low,j=high;
if(i>=j)
{
return ;
}
int mid=(i+j)/2;//二分
Sqrt(i,mid);
Sqrt(mid+1,j);
Link_Sqrt(i,mid,mid+1,j);//归并
} int main()
{
while(scanf("%d",&n),n)
{
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum=0;
Sqrt(0,n-1);
printf("%lld\n",sum);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Ultra-QuickSort 分类: POJ 排序 2015-08-03 15:39 2人阅读 评论(0) 收藏的更多相关文章
- 8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏
排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...
- DZY Loves Chemistry 分类: CF 比赛 图论 2015-08-08 15:51 3人阅读 评论(0) 收藏
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- UI基础:UIButton.UIimage 分类: iOS学习-UI 2015-07-01 21:39 85人阅读 评论(0) 收藏
UIButton是ios中用来响应用户点击事件的控件.继承自UIControl 1.创建控件 UIButton *button=[UIButton buttonWithType:UIButtonTyp ...
- javascript中0级DOM和2级DOM事件模型浅析 分类: C1_HTML/JS/JQUERY 2014-08-06 15:22 253人阅读 评论(0) 收藏
Javascript程序使用的是事件驱动的设计模式,为一个元素添加事件监听函数,当这个元素的相应事件被触发那么其添加的事件监听函数就被调用: <input type="button&q ...
- XHTML 结构化:使用 XHTML 重构网站 分类: C1_HTML/JS/JQUERY 2014-07-31 15:58 249人阅读 评论(0) 收藏
http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...
- 各种排序算法的分析及java实现 分类: B10_计算机基础 2015-02-03 20:09 186人阅读 评论(0) 收藏
转载自:http://www.cnblogs.com/liuling/p/2013-7-24-01.html 另可参考:http://gengning938.blog.163.com/blog/sta ...
- Hadoop常见异常及其解决方案 分类: A1_HADOOP 2014-07-09 15:02 4187人阅读 评论(0) 收藏
1.Shell$ExitCodeException 现象:运行hadoop job时出现如下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : at ...
- Basic 分类: POJ 2015-08-03 15:49 3人阅读 评论(0) 收藏
Basic Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 905 Accepted: 228 Description The p ...
- 选择排序 分类: 算法 c/c++ 2014-10-10 20:32 509人阅读 评论(0) 收藏
选择排序(假设递增排序) 每次选取从当前结点到末尾结点中最小的一个与当前结点交换,每一轮固定一个元素位置. 时间复杂度O(n^2),空间复杂度O(1).下面的示例代码以带头结点的链表为存储结构: #i ...
随机推荐
- Idea 安装 lombok
idea 目前是Java开发者最流行的一款编辑器.为了让java开发更加的简便idea 也提供了lombok的插件. 插件的按钮方式为: 1.进入idea的.setting面板 2.按照以下图进行操作 ...
- java io读书笔记(3)数值类型的数据
input stream读取字节:out stream写入字节.Readers读取字符而Writers写入字符.因此,如果我们想理解input和output,我们首先就要明白 java如何处理字节,整 ...
- 实验10.3_数值显示拓展_dword型数转变为表示十进制数的字符串
assume cs:code data segment db 10 dup (0) data ends code segment start : mov ax,4240H;F4240H=1000000 ...
- Leetcode: Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- CCF真题之相反数
201403-1 问题描述 有 N 个非零且各不相同的整数.请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数). 输入格式 第一行包含一个正整数 N.(1 ≤ N ≤ 500). ...
- centos的网路配置文件的位置
在这个路径下:/etc/sysconfig/network-scripts/ 每个网卡有相应的配置文件
- ASP.NET MVC API 路由生成规则
我们都知道调用ASP.NET MVC的某些API函数(诸如:Url.Action.RedirectToAction等)可以生成URL,ASP.NET MVC会根据调用API函数时传入的参数去匹配系统定 ...
- HTTP请求流程(一)----流程简介
最近一直在研究如何让asp.net实现上传大文件的功能,所以都没怎么写技术类的文章了.可惜的是至今还没研究出来,惭愧~~~.不过因为这样,也了解了一下http消息请求的大致过程.我就先简单介绍下,然后 ...
- Javascript 类与静态类的实现-js面向对象
在Javascript里,对面向对象并没有一个直接的实现,对于代码方面也是非常的灵活. 今天所要说的就是,如何在Javascript里写类与静态类,这是本人一惯用的方法,你也可以有更为方便的,也可以发 ...
- MAC常用快捷键
1.F11 用于当前界面与桌面进行切换,Command-M 最小化窗口,Option-Command-M 最小化所有窗口,Command-数字 切换数字指定的网页列表 Shift-Command-D ...