sort

Time Limit : 6000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

给你n个整数,请按从大到小的顺序输出其中前m大的数。

Input

每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。

Output

对每组测试数据按从大到小的顺序输出前m大的数。

Sample Input

5 3
3 -35 92 213 -644

Sample Output

213 92 3

Hint

Hint
请用VC/VC++提交

Author

LL

Source

ACM暑期集训队练习赛(三)

 
  排序算法,综合练习。
  这道题可以作为排序算法的综合练习,题意很简单,没有那么多弯弯绕绕,对n个数进行排序,然后输出前m个大的数。数据规模很大,冒泡会超时。有疑惑的话可以用下方冒泡排序代码测试一下。
 #include <iostream>
#include <stdio.h>
using namespace std;
int a[];
int main()
{
int n,m;
while(cin>>n>>m){
int i,j;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n-;i++)
for(j=;j<=n-i;j++)
if(a[j]>a[j+]){
int t;
t=a[j];a[j]=a[j+];a[j+]=t;
}
for(i=n;i>=n-m+;i--){
if(i==n-m+)
cout<<a[i]<<endl;
else
cout<<a[i]<<' ';
}
}
return ;
}

  惭愧的是,为了省事,这道题我直接套用了<algorithm>中的sort()函数,从网上得知该函数使用的是内省排序,时间复杂度是O(nlogn),应付这道题是够了,时间用了500+MS。当然你也可以采用快排,时间复杂度也是O(nlogn),对付这道题也没问题。

  注意:输出前m个数的时候,最后一个数后面不能带空格' ',否则提交会格式错误。

  有时间会重新用这道题练习一下,低效率的算法通过不了这道题。

 #include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[];
int main()
{
int n,m;
while(cin>>n>>m){
int i;
for(i=;i<=n;i++) //输入
scanf("%d",&a[i]);
sort(a+,a+n+); //从小到大排序
for(i=n;i>=n-m+;i--){ //输出前m个大的数
if(i==n-m+)
cout<<a[i]<<endl;
else
cout<<a[i]<<' ';
}
}
return ;
}
Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
10133960 2014-02-20 15:49:42 Accepted 1425 593MS 2284K 343 B C++ freecode

 

  正好看到数据结构内排序这一部分,想起了这道题,拿它来练习快速排序正好。

  注意:输入的时候尽量用scanf(),用cin会超时。

  快排模板

 void quicksort(int a[],int s,int t)    //对a[]的第s个到第t个元素进行从小到大的排序
{
int i=s,j=t;
int tmp = a[s];
if(s<t){ //区间内元素剩0个或者1个的时候停止
while(i<j){
while(i<j && a[j]>=tmp)
j--;
a[i] = a[j];
while(i<j && a[i]<=tmp)
i++;
a[j] = a[i];
}
a[i] = tmp;
quicksort(a,s,i-); //对左区间递归排序
quicksort(a,i+,t); //对右区间递归排序
}
}

  本题代码

 #include <iostream>
#include <stdio.h>
using namespace std;
int a[];
void quicksort(int a[],int s,int t)
{
int i=s,j=t;
int tmp = a[s];
if(s<t){ //区间内元素剩0个或者1个的时候停止
while(i<j){
while(i<j && a[j]>=tmp)
j--;
a[i] = a[j];
while(i<j && a[i]<=tmp)
i++;
a[j] = a[i];
}
a[i] = tmp;
quicksort(a,s,i-); //对左区间递归排序
quicksort(a,i+,t); //对右区间递归排序
}
}
int main()
{
int i;
int n,m;
while(cin>>n>>m){
for(i=;i<=n;i++)
scanf("%d",&a[i]);
quicksort(a,,n);
for(i=n;i>=n-m+;i--){
printf("%d",a[i]);
if(i!=n-m+)
printf(" ");
else
printf("\n");
}
}
return ;
}
Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
10361233 2014-03-21 19:40:50 Accepted 1425 468MS 2276K 693 B C++ freecode

Freecode : www.cnblogs.com/yym2013

hdu 1425:sort(排序,经典题。快排模板)的更多相关文章

  1. E题hdu 1425 sort

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 sort Time Limit: 6000/1000 MS (Java/Others)    M ...

  2. leetcode 75 Sort Colors 计数排序,三路快排

    解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k)    k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...

  3. hdu - 6281,2018CCPC湖南全国邀请赛F题,快排

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6281 题意: 根据已给出的式子,进行排序,然后输出排完序后原先的下表. 题解:用结构体保存,在用结构体 ...

  4. C语言实现单向链表及其各种排序(含快排,选择,插入,冒泡)

    #include<stdio.h> #include<malloc.h> #define LEN sizeof(struct Student) struct Student / ...

  5. HDU 1425 sort(堆排序/快排/最大堆/最小堆)

    传送门 Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不 ...

  6. hdu 1425 sort 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...

  7. HDU 1425 sort hash+加速输入

    http://acm.hdu.edu.cn/showproblem.php?pid=1425 题目大意: 给你n个整数,请按从大到小的顺序输出其中前m大的数. 其中n和m都是位于[-500000,50 ...

  8. HDU 1425 sort 【哈希入门】

    题意:给出n个数,输出前m大的数 和上一题一样,将输入的数加上一个极大地值作为地址 #include<iostream> #include<cstdio> #include&l ...

  9. HDU 1425 sort 题解

    选择出数列中前k个最大的数. 这里由于数据特殊.所以能够使用hash表的方法: #include <cstdio> #include <algorithm> #include ...

随机推荐

  1. 最全的iOS物理引擎demo

    概述 最全的iOS物理引擎demo,实现重力.碰撞.推力.摆动.碰撞+重力.重力弹跳.仿摩拜单车贴纸效果.防iMessage滚动效果.防百度外卖首页重力感应等效果! 详细 代码下载:http://ww ...

  2. 【CentOS6.5】安装nginx报错:No package nginx available. Error: Nothing to do

    今天在给centos6.5安装nginx时候,提示报错No package nginx available. Error: Nothing to do, 后来百度一下,说缺少EPEL(epel是社区强 ...

  3. Redis全方位讲解--哨兵模式(Sentinel模式)(转载)

    前言 当按照上一篇<redis主从复制>部署好之后,我们会想,一旦redis的master出现了宕机,并且我们并没有及时发现,这时候就可能会出现数据丢失或程序无法运行.此时,redis的哨 ...

  4. 自增长主键Id的设计

    http://www.cnblogs.com/lhking/p/3945865.html

  5. poj 1236 Network of Schools 【Tarjan】

    题目链接:http://poj.org/problem?id=1236 题意: 本题为有向图. 需解决两个问题: 1 须要给多少个点,才干传遍全部点. 2 加多少条边,使得整个图变得强连通. 使用Ta ...

  6. 构建SqlSessionFactory 的过程

    1 SqlSessionFactory 的核心功能是创建 SqlSession 接口,而 SqlSessionFactory 是通过 SqlSessionFactoryBuilder 去构建. 构建步 ...

  7. mysql numberic types ---- mysql 数值类型详解

    编程语言中大多都有数据类型一说.虽然mysql 的sql 语句与标准sql 有别.但是宏观上看还是差不多的:下面我们说一下mysql数据库中的数值类型 一.在mysql里有那些类型可以表示数值: 1. ...

  8. AngularJS 中 Controller 之间的通信

    用 Angular 进行开发,基本上都会遇到 Controller 之间通信的问题,本文对此进行一个总结. 在 Angular 中,Controller 之间通信的方式主要有三种: 1)作用域继承.利 ...

  9. spawn-fcgi原理及源代码分析

    spawn-fcgi是一个小程序,作用是管理fast-cgi进程,功能和php-fpm类似,简单小巧,原先是属于lighttpd的一部分.后来因为使用比較广泛.所以就迁移出来作为独立项目了.本文介绍的 ...

  10. 响应式布局框架 Pure-CSS 5.0 示例中文版-中

    8. 表单 Form 在 form 标签中添加 .pure-form 类,可生成单行表单(inline) 效果图: 代码: <form class="pure-form"&g ...