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 ...
随机推荐
- Silverlight TabItem选中,未选中样式设置
<Application.Resources> <LinearGradientBrush x:Key="TabItemBackground" EndPoint=& ...
- 与PostgreSQL相关的工具
Pentaho Data Integration(kettle):一个优秀的抽取.转换.加载(Extract Transform and Load,ETL)工具 Pentaho Report Ser ...
- C++ set容器简单用法
set是关联容器,类似于集合,里面的元素不会重复,而且呈现为有序性 常用操作: using namespace std; set<int>:s;1.元素插入:s.insert()2.中序遍 ...
- C#删除xml中某个节点的子节点方法
if (File.Exists(xmlFilePath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); Xm ...
- 安装Git
因为我的开发环境是Windows,那么在windows下安装Git就简单很多了.首先在,从http://msysgit.github.io/下载Git的安装程序,然后按默认选项安装即可. 安装成功之后 ...
- UVA 10891 Game of Sum(DP)
This is a two player game. Initially there are n integer numbers in an array and players A and B get ...
- URAL 1002 Phone Numbers(KMP+最短路orDP)
In the present world you frequently meet a lot of call numbers and they are going to be longer and l ...
- linux env
.Linux的变量种类 按变量的生存周期来划分,Linux变量可分为两类: 1.1 永久的:需要修改配置文件,变量永久生效. 1.2 临时的:使用export命令声明即可,变量在关闭shell时失效. ...
- paper 17 : 机器学习算法思想简单梳理
前言: 本文总结的常见机器学习算法(主要是一些常规分类器)大概流程和主要思想. 朴素贝叶斯: 有以下几个地方需要注意: 1. 如果给出的特征向量长度可能不同,这是需要归一化为通长度的向量(这里以文本分 ...
- Android真机测试 INSTALL_FAILED_INSUFFICIENT_STORAGE 解决方法[转]
方法一: 试试修改一下manifest文件 :添加 一句: android:installLocation="preferExternal" [html]view plainc ...