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. [转发]jQuery Validation范例

    验证操作类formValidatorClass.js参照文件有: http://www.cnblogs.com/easyinsc/archive/2009/02/27/1407826.html htt ...

  2. JDBC JdbTemplate&NamedParameterJdbcTemplate(Spring工具类)

    使用该工具类需要从spring开发包中导入spring.jar和commons-logging.jar,这个模板是线程安全的.   JdbcTemplate: public class JdbcTem ...

  3. Appium环境的安装与配置,Python测试脚本测试

    Appium自动化测试系列1 - Appium环境的安装与配置 发表于4个月前(2015-01-27 14:34)   阅读(803) | 评论(0) 0人收藏此文章, 我要收藏 赞0 寻找 会’偷懒 ...

  4. NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)

    本篇文章由:http://xinpure.com/nsurlsessionnsurlconnection-http-load-failed-kcfstreamerrordomainssl-9802/ ...

  5. linux配置java环境变量(详细) [转]

    一. 解压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586.bin 这时会出现一段协议,连继敲回车, ...

  6. npoi IWorkbook HSSFWorkbook XSSFWorkbook 拥有 IEnumerator GetEnumerator(); 方法 可以遍历workbook 每个元素为每个sheet页

  7. python笔记-字符串

    >>> myString = 'hello world !' >>> print myString # print语句会调用str()函数 hello world ...

  8. STL六大组件简介

    一.STL简介 (一).泛型程序设计 泛型编程(generic programming) 将程序写得尽可能通用 将算法从数据结构中抽象出来,成为通用的 C++的模板为泛型程序设计奠定了关键的基础 (二 ...

  9. Django URL中r的意思

    例如: urlpatterns = patterns('', # ... (r'^time/plus/\d+/$', hours_ahead), # ... ) 正则表达式字符串的开头字母“r”. 它 ...

  10. mysql create dabase 语法详解

    由于SQL标准的存在,各个关系型数据库管理系统中创建库的语句都差不多 一.mysql 中创建数据库的语法如下: 1.创建数据库的语法: create {database | schema } [if ...