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. Rust的几个预测

    写程序多年,语言也用过不下十种,对于Rust有种亲人的感觉,就像在梦中见到过似的.现在对于Rust特做出以下一些预测,希望Rust会有更大的影响力. 1. 当前的Rust的核心功能现以比较稳定,可以用 ...

  2. robotframework ride安装

    之前在python3.3.5的环境下一直无法找到匹配的wxPython版本,只能再装了一个python2.7,后面在2.7的环境下重新安装了robotframework和ride,结果还是无法启动ri ...

  3. Redis在windows下安装和PHP中使用

    1.redis简介redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...

  4. Struts框架——(一)用Servlet + JSP演示Struts基本原理

    一. 用Servlet + JSP演示Struts基本原理 struts是开源项目.它通过采用 Java Servlet/JSP 技术,实现了基于Java EE Web应用的MVC的应用框架.Stru ...

  5. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  6. 细说;(function ($, undefined){ })(jQuery); 的使用

    1. 对于function前面的分号(;)的使用:使用分号的目的是为了防止多个文件压缩合并时,以为其他文件最后一行语句没加分号,而引起合并后的语法错误. 2. (function ($, undefi ...

  7. How to build the Robotics Library from source code on Windows

    The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...

  8. Android 遮罩层效果

    (用别人的代码进行分析) 不知道在开发中有没有经常使用到这种效果,所谓的遮罩层就是给一张图片不是我们想要的形状,这个时候我们就可以使用遮罩效果把这个图片变成我们想要的形状,一般使用最多就是圆形的效果, ...

  9. ExtJs知识点概述

    1.前言 ExtJS的前身是YUI(Yahoo User Interface).经过不断的发展与改进,ExtJS现在已经成功发布到了ExtJS 6版本,是一套目前最完整和最成熟的javascript基 ...

  10. nfs:环境搭建

    准备环境 通过VirtualBox创建两台虚拟机client1和client2,这两台虚拟机和物理主机组成一个网络.将物理主机作为NFS的服务端,虚拟机client1和client2作为NFS的客户端 ...