HDU1425 <sort 快排>
给你n个整数,请按从大到小的顺序输出其中前m大的数。
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。<快排>
//sort也可以过//936Ms
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int ans[maxn];
bool cmp(int a,int b)
{
return a>b;
}
int main ()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&ans[i]);
sort(ans, ans+n, cmp);
for(int i=0;i<m-1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[m-1]);
}
return 0;
}
//快排1//873Ms
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int ans[maxn];
void quickSort(int l,int r)
{
if(l>=r)
return ;
int i,j;
for(j=i=l+1;i<=r;i++)
if(ans[i]>ans[l])
swap(ans[i],ans[j++]);
swap(ans[l],ans[--j]);
quickSort(l, j-1);
quickSort(j+1, r);
}
/*bool cmp(int a,int b)
{
return a>b;
}*/
int main ()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&ans[i]);
//sort(ans, ans+n, cmp);
quickSort(0, n-1);
for(int i=0;i<m-1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[m-1]);
}
return 0;
}
//快排2//982ms
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int ans[maxn];
void quickSort(int l,int r)
{
if(l>=r)
return ;
int left=l,right=r,temp=ans[left];
while(left<right)
{
while(left<right&&ans[right]<=temp)
right--;
ans[left]=ans[right];
while(left<right&&ans[left]>=temp)
left++;
ans[right]=ans[left];
}
ans[left]=temp;
quickSort(l,left-1);
quickSort(left+1, r);
}
/*bool cmp(int a,int b)
{
return a>b;
}*/
int main ()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&ans[i]);
//sort(ans, ans+n, cmp);
quickSort(0, n-1);
for(int i=0;i<m-1;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[m-1]);
}
return 0;
}
//快排3//624Ms
#include <iostream>
#include <string.h>
#include <cstdio>
#include<algorithm>
using namespace std;
const int N=1000005;
int num[N];
int partition(int low,int high){
int i=low,j=high,key=num[low];
while(i<j){
while(i<j&&num[j]>key)
--j;
swap(num[i],num[j]);
//int t=num[i]; num[i]=num[j];num[j]=t;
while(i<j&&num[i]<key)
++i;
swap(num[i],num[j]);
//t=num[i]; num[i]=num[j];num[j]=t;
}
return i;
}
void quick_sort(int low,int high){
if(low<high){
int x=partition(low,high);
quick_sort(low,x-1);
quick_sort(x+1,high);
}
}
int main(){
//freopen("11.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<n;++i)
scanf("%d",&num[i]);
quick_sort(0,n-1);
for(int i=n-1;i>n-m;--i)
printf("%d ",num[i]);
printf("%d",num[n-m]);
printf("\n");
}
return 0;
}
HDU1425 <sort 快排>的更多相关文章
- Quick Sort(快排)
这是挖坑填补法的演示 快排之挖坑填补法: void Quick(int top/*起始位置*/,int end/*末尾位置*/,int arr[])//挖坑填补法 { int i=top,j=end, ...
- sort 快排解决百万级的排序
问题:给n个整数,按从大到小的顺序,输出前m大的整数0<m,n<1000000,每个整数[-500000,500000]输入:5 33 -35 92 213 -644输出:213 92 3 ...
- 函数模拟sort快排
设计一个对一维数组进行排序的sort函数,并调用它实现数组排序 思路:函数调用不止调用一个,最主要对函数不熟悉: #include<stdio.h> #define N 10 int ma ...
- java链表实现快排
链表文件 package sort; public class SqList { public int LIST_INIT_SIZE = 8;//链表的原始大小 private int I ...
- 结构体快排回顾(sort)
一般来说,我做竞赛的时候排序一般用快排 很快很方便 普通sort(从小到大) sort(a,a+n); 直接贴一段代码吧,包含了vector,sort,结构体等简单东西综合 #include < ...
- JavaScript快排与原生sort的测试
今天工作室断网!果断回宿舍,不然各种资料都没有.(他说将来会找到!)不好意思,又哼起来了.进入主题,大家都知道,快排是各种排序算法中,最高效的也是应用最广的,还有更重要的一点,面试特别爱考的! 其实大 ...
- 阮一峰大神的快排?刚才还在纠结sort()的我!真是个小傻瓜
看到这个标题之后 我毫不犹豫的点进去了 趁现在不忙我赶紧把代码写到了我的小本本上好好研究研究 (写的就不放进来了 有点丑) 研究了下 第一反应 明明能用sort()解决的 为什么非要写这么一大串 但 ...
- 待字闺中之快排单向链表;leetcode之Sort List
题目来源.待字闺中.原创@陈利人 .欢迎大家继续关注微信公众账号"待字闺中" 分析:思路和数据的高速排序一样,都须要找到一个pivot元素.或者节点. 然后将数组或者单向链表划分为 ...
- hdu 1425:sort(排序,经典题。快排模板)
sort Time Limit : 6000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissi ...
随机推荐
- 如何使用SecureCRT连接ubuntu
1. 首先要明白什么是ssh? 可以把ssh看做是telnet的加强版,telnet的密码和信息都是不加密的,而ssh则加密. .2. 开启ubuntu上的ssh功能 先安装,安装后就自动开启了. s ...
- [Lua]Lua高级教程Metatables
什么是Metatable metatable是Lua中的重要概念,每一个table都可以加上metatable,以改变相应的table的行为. Metatables举例 -- 声明一个正常的关系变量 ...
- Linux系统的信号详解
一.信号类型 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) ...
- 制作 macOS Sierra 正式版U盘USB启动安装盘方法教程 (全新安装 Mac 系统)
使用命令行创建制作 macOS Sierra 正式版 USB 安装盘 1.准备一个 8GB 或更大容量的 U盘,并备份好里面的所有资料. 2.下载好 macOS Sierra 正式版的安装程序(app ...
- c_select 调用参数说明
c_select 调用 1. select系统调用select系统调用是用来让我们的程序监视多个文件描述符的状态变化的.程序会停在select这里等待,直到被监视的文件描述符有某一个或多个发生了状态改 ...
- jsp:usebean 常用注意事项
bean就是一个Java类,一般来说写这个类应该注意以下几点: 1.必须有包名,不要使用裸类 2.类的第一个名字小写 3.尽量不要使用公共成员变量,使用私有的,通过set.get方法来操作类中的变量 ...
- 关于Winform中的用户代理
问题描述: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 解释: 1. 应用程序版本“Mozilla ...
- win8.1下安装ubuntu 14.0 4LTS
1.前奏 电脑上已经安装了win8.1系统 2.准备工作 关闭win8.1的快速启动 步骤: 控制面板->电源选项->选择电源按钮的功能->更改不可用的设置,然后把"启用快 ...
- Hibernate 系列教程15-一级缓存
Product public class Product { private Long id; private String name; Product.hbm.xml <class name= ...
- js管理内存
数据不再有用时,最好通过将其值置NULL来释放其引用-这个做法叫做解除引用(dereference).这个做法适用于全局变法和全局对象的属性. function createPerson(name){ ...