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. 手机页面touch触摸事件

    请看示例: <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset=" ...

  2. socket阻塞与非阻塞,同步与异步

    socket阻塞与非阻塞,同步与异步 作者:huangguisu 转自:http://blog.csdn.net/hguisu/article/details/7453390 1. 概念理解 在进行网 ...

  3. weblogic部署脚本

    #!/bin/bash #-- #writen lxh dir_war=/home/weblogic/war dir_app=/servyouapp/weblogic/user_projects/do ...

  4. Monkeyrunner小脚本关于camera的使用

    一下代码涉及自动执行camera,属性一个小设置,恢复初始值,并在中间添加截屏功能 将两个截屏进行前后对比,并返回值 适合初学者,刚刚了解monkeyrunner 的人员来看 注意:一下脚本如果不能执 ...

  5. keepalived高可用

    keepalived介绍 Keepalived是一个基于vrrp协议的高可用方案,vrrp协议的软件实现,原生设计的目的为了高可用ipvs服务. 1. vrrp协议 VRRP是一种容错协议,它通过把几 ...

  6. JAVA第4次作业

    package fuzhi; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOExc ...

  7. 在 Linux 下将 PNG 和 JPG 批量互转的四种方法

    计算机术语中,批处理指的是用一个非交互式的程序来执行一序列的任务[1]的方法.这篇教程里,我们会使用 Linux 命令行工具,并提供 4 种简单的处理方式来把一些 .PNG 格式的图像批量转换成 .J ...

  8. SQL Server 常用函数

    1.DATEADD 在向指定日期加上一段时间的基础上,返回新的 datetime 值. 语法 DATEADD ( datepart , number, date ) 参数 datepart 是规定应向 ...

  9. [[UIScreen mainScreen] bounds]获取尺寸不对

    http://www.cocoachina.com/bbs/read.php?tid-264373.html [[UIScreen mainScreen] bounds]获取尺寸不对       我在 ...

  10. IIS配置excel 权限

    http://www.cnblogs.com/zhuxiaohui/archive/2013/10/16/3371637.html