sort

Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25803    Accepted Submission(s): 7764

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
 

最简单的运用了哈希表的思想的题,开始看了好多关于散列的理论资料,太深奥反而抓不住基本思想。

基本做法

 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; bool cmp(int x,int y)
{
return x>y;
} int a[]; int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
for(int i = ;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
printf("%d",a[]);
for(int i = ;i<k;i++)
printf(" %d",a[i]);
printf("\n");
} return ;
}

哈希表思想(空间换时间)

 #include <iostream>
#include <cstring>
#include <stdio.h>
using namespace std; int hash[];
int main()
{
int i,n,m,t;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(hash,,sizeof(hash));
for(i=;i<n;i++)
{
scanf("%d",&t);
hash[t+]=;
}
int flag=;
for(i=;i>;i--)
{
if(hash[i])
{
printf("%d",i-);
m--;
if(m)
printf(" ");
}
if(m==) break;
}
printf("\n");
}
return ;
}

对比:


上为哈希表,下为普通做法。可看出差别很大

hdu acm 1425 sort(哈希表思想)的更多相关文章

  1. HDU 1496 Equations(哈希表)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1496 [题目大意] 给出一个方程ax1^2+bx2^2+cx3^2+dx4^2=0,求-100到1 ...

  2. leetcode_438_Find All Anagrams in a String_哈希表_java实现

    题目: Given a string s and a non-empty string p, find all the start indices of p's anagrams in s. Stri ...

  3. E题hdu 1425 sort

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

  4. hdu 5183. Negative and Positive (哈希表)

    Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  5. HDU4887_Endless Punishment_BSGS+矩阵快速幂+哈希表

    2014多校第一题,当时几百个人交没人过,我也暴力交了几发,果然不行. 比完了去学习了BSGS才懂! 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4887 ...

  6. 【哈希表】CodeVs1230元素查找

    一.写在前面 哈希表(Hash Table),又称散列表,是一种可以快速处理插入和查询操作的数据结构.哈希表体现着函数映射的思想,它将数据与其存储位置通过某种函数联系起来,其在查询时的高效性也体现在这 ...

  7. hdu acm 1028 数字拆分Ignatius and the Princess III

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  8. [转]net中哈希表的使用 Hashtable

    本文转自:http://www.cnblogs.com/gsk99/archive/2011/08/28/2155988.html 以下是PetShop中DBHelper中的使用过程: //创建哈希表 ...

  9. Snowflake Snow Snowflakes(哈希表的应用)

    Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 27312   Accep ...

随机推荐

  1. HTTP Error 503. The service is unavailable

    网站运行一段时间后,突然所有的页面都报告以下错误: HTTP Error 503. The service is unavailable 经检查,应用程序池自动停止,可能是工作进程抛出的异常数超出限制 ...

  2. css学习中的一些英文单词

    indent 缩进 through通过 decoration装饰 position定位

  3. NSDate 刚刚、几分钟、几小时

    - (NSString *)minutesAgo { NSDateFormatter *fmt = [[NSDateFormatter alloc] init]; fmt.dateFormat = @ ...

  4. Singleton in C++11 style

    #include <iostream> #include <memory> #include <mutex> class SingletonOld { static ...

  5. Hibernate控制台显示创建数据库表语句

    package cqvie.yjq.View; import org.hibernate.Session; import org.hibernate.Transaction; import org.h ...

  6. 【随笔】Photoshop简单切图

    一.首先准备一张需要切割的图: 二.打开Photoshop,在ps里打开这张图片: 三.在界面的左边选择切片工具: 四.根据需要,鼠标点住上方和左边的标尺拖动,给图片加上分割辅助线: 五.用切片工具点 ...

  7. iOS.Performance-trick-presentViewController-is-so-slow-in-didSelectRowAtIndexPath

    presentViewController is so slow in "tableView:didSelectRowAtIndexPath:" Use Case: 在UITabl ...

  8. Asp.Net MVC4入门指南(6):验证编辑方法和编辑视图

    在本节中,您将开始修改为电影控制器所新加的操作方法和视图.然后,您将添加一个自定义的搜索页. 在浏览器地址栏里追加/Movies, 浏览到Movies页面.并进入编辑(Edit)页面. Edit(编辑 ...

  9. LXD 2.0 系列(四):资源控制

    LXD 提供了各种资源限制.其中一些与容器本身相关,如内存配额.CPU 限制和 I/O 优先级.而另外一些则与特定设备相关,如 I/O 带宽或磁盘用量限制.-- Stéphane Graber 本文导 ...

  10. windows下搭建scrapywindows 7 (64) + python 3.5 (64)

    说明 之前在 window 10 (64) + python 3.5 (64) 环境下就已经成功安装了 scrapy,当然也费了不少周折. 由于近日将系统换回 windows 7 (64),再安装 s ...