Ultra-QuickSort (poj 2002)
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
Output
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0
这题很简单的样子,就是求冒泡排序的交换次数,but 超时
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int s[],temp[];
long long cut;//这里害我WA了一次
void merge_sort(int* A,int x,int y,int* T )
{
if(y-x>)
{
int m=x+(y-x)/;//划分
int p=x,q=m,i=x;
merge_sort(A,x,m,T);//递归求解
merge_sort(A,m,y,T);
while(p<m||q<y)
{
if(q>=y||(p<m&&A[p]<=A[q]))
T[i++]=A[p++];//从左半数组复制到临时空间
else
{
T[i++]=A[q++];//从右半数组复制到临时空间
cut+= (m-p);//统计逆序数
}
}
for(i=x; i<y; i++)
A[i]=T[i];//从辅助数组复制回原数组
}
}
int main()
{
int n,i;
while(scanf("%d",&n)&&n)
{
cut=;
for(i=; i<n; i++)
scanf("%d",&s[i]);
merge_sort(s,,n,temp);
printf("%lld\n",cut);
}
return ;
}
#include<iostream>
using namespace std;
long long cnt;
void merge(int array[],int left,int mid,int right)
{
int* temp=new int[right-left+];
int i,j,p;
for(i=left,j=mid+,p=; i<=mid&&j<=right; p++)
{
if(array[i]<=array[j])temp[p]=array[i++];
else temp[p]=array[j++],cnt+=(mid-i+);
}
while(i<=mid)temp[p++]=array[i++];
while(j<=right)temp[p++]=array[j++];
for(i=left,p=; i<=right; i++)array[i]=temp[p++];
delete temp;
}
void mergesort(int array[],int left,int right)
{
if(left==right)array[left]=array[right];
else
{
int mid=(left+right)/;
mergesort(array,left,mid);
mergesort(array,mid+,right);
merge(array,left,mid,right);
}
}
int main()
{
int n,array[];
while(cin>>n&&n)
{
cnt=;
for(int i=; i<n; i++)
cin>>array[i];
mergesort(array,,n-);
cout<<cnt<<endl;
}
return ;
}
下面这个是网上找的还算好懂得,耗时是我敲得那个的10倍左右
Hint
Ultra-QuickSort (poj 2002)的更多相关文章
- 雷达装置 (POJ 1328/ codevs 2625)题解
[问题描述] 假定海岸线是一条无限延伸的直线,陆地在海岸线的一边,大海在另一侧.海中有许多岛屿,每一个小岛我们可以认为是一个点.现在要在海岸线上安装雷达,雷达的覆盖范围是d,也就是说大海中一个小岛能被 ...
- 最后一个非零数字(POJ 1604、POJ 1150、POJ 3406)
POJ中有些问题给出了一个长数字序列(即序列中的数字非常多),这个长数字序列的生成有一定的规律,要求求出这个长数字序列中某个位上的数字是多少.这种问题通过分析,找出规律就容易解决. 例如,N!是一个非 ...
- POJ中和质数相关的三个例题(POJ 2262、POJ 2739、POJ 3006)
质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能被其他自然数整除,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数. 最小的质数 ...
- POJ 2002 Squares 解题报告(哈希 开放寻址 & 链式)
经典好题. 题意是要我们找出所有的正方形.1000点,只有枚举咯. 如图,如果我们知道了正方形A,B的坐标,便可以推测出C,D两点的坐标.反之,遍历所有点作为A,B点,看C,D点是否存在.存在的话正方 ...
- (多重背包+记录路径)Charlie's Change (poj 1787)
http://poj.org/problem?id=1787 描述 Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie dri ...
- POJ 2002 统计正方形 HASH
题目链接:http://poj.org/problem?id=2002 题意:给定n个点,问有多少种方法可以组成正方形. 思路:我们可以根据两个点求出对应正方形[有2个一个在两点左边,一个在两点右边] ...
- 种类并查集(POJ 1703)
1703 -- Find them, Catch them http://poj.org/problem?id=1703 题目大意:有2个敌对帮派,输入D a b表示a,b在不同帮派,输入A a b表 ...
- 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)
Charm Bracelet POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...
- Scout YYF I(POJ 3744)
Scout YYF I Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5565 Accepted: 1553 Descr ...
随机推荐
- 怎样在delphi中实现控件的拖拽
下面这2种方法都能实现对控件和窗体的拖拽 方法1 procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift ...
- Qt解析XML文件(QXmlStreamReader)
(2013-08-03 10:53:53) 转载▼ 如何使用QXmlStreamReader来解析格式良好的XML,Qt的文档中指出,它是一种更快.更方便的Qt自己的SAX解析器(QXml ...
- iOS--为视图添加阴影
iOS–为视图添加阴影 情况一:视图添加圆角,在添加阴影 //阴影视图 self.viewShadow = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ...
- [iOS开发] 使用第三方字体不生效
iOS中使用第三方字体并不复杂,通常只需要如下三个步骤: 1. 将第三方字体文件添加到工程(Project)中: 2. 在info.plist中添加一个新的键"Fonts provided ...
- IE兼容问题
1.IE下event事件没有target属性,只有srcElement属性,解决方法:使用srcObj = event.srcElement ? event.srcElement : event.ta ...
- Orchard路由随记(一)
对于Orchard来说,个人以为要真正理解Orchard,必须理解其路由工作方式. 一.Orchard的自定义路由由三种类型组成 1.分发类: HubRoute:其功能是按租户筛选出当前访问租户的路由 ...
- HTML5 文件域+FileReader 读取文件并上传到服务器(三)
一.读取文件为blob并上传到服务器 HTML <div class="container"> <!--读取要上传的文件--> <input type ...
- C#Socket编程socket.Connect权限出错问题及解决
最近使用Vs2010编写Socket程序,客户端在调用socket.Connect()时,总是出现: 请求“System.Net.SocketPermission, System, Version=4 ...
- iOS LaunchScreen设置启动图片,启动页停留时间
[新建的iOS 项目启动画面默认为LaunchScreen.xib] 如果想实现一张图片作为启动页,如下图
- Linux通配符
* 任意字符 ?任意单个字符 [] 匹配指定 字符范围内的字符 [^] 指定范围之外的单个字符 常规字符集合 [a-z] a到z的所有小写字母 [A-Z] a到z的所有大写字母 [0-9] 0到9的所 ...