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. LinckedhashMap原理

    http://zhangshixi.iteye.com/blog/673789 TreeMap的key是有顺序的,是自然顺序,也可以指定比较函数. 但默认不是按插入的顺序.  为了让Map  JSON ...

  2. Spring+springmvc+Mybatis整合案例 annotation版(myeclipse)详细版

    Spring+springmvc+Mybatis整合案例 Version:annotation版 文档结构图: 从底层开始做起: 01.配置web.xml文件 <?xml version=&qu ...

  3. 使用Word发布文章到 WordPress 博客

    使用Word发布文章到 WordPress 博客 我们都知道,WordPress 自带的编辑器功能比较弱,而使用 Word 编辑文档却功能强大.其实我们使用 Word 编辑好的文档也是可以直接发布到 ...

  4. CocosCreator反射在Android中的使用

    CocosCreator反射在Android中的使用 新建一个CocosCreator项目,然后点击构建 构建完成之后,即可用AndroidStudio打开构建的项目 使用AndroidStudio打 ...

  5. maven project 更新总是jre-1.5

    解决如下: <build>    <plugins>          <plugin>                <groupId>org.apa ...

  6. SQL语句中=null和is null

    平时经常会遇到这两种写法:IS NOT NULL与!=NULL.也经常会遇到数据库有符合条件!=NULL的数据,但是返回为空集合.实际上,是由于对二者使用区别理解不透彻. 默认情况下,推荐使用 IS ...

  7. 利用JDK(1.6及以上版本)创建WebService

    一.什么是WebService WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络 ...

  8. POJ 3624 Charm Bracelet(01背包)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34532   Accepted: 15301 ...

  9. winAPI 检查系统设备拔插使用 WM_DEVICECHANGE 消息

    if(message->message == WM_DEVICECHANGE) { /*if (message->wParam == DBT_DEVICEARRIVAL || messag ...

  10. python 整齐输出与编码读写

    # -*- coding:utf-8 -*- # Author:mologa for x in range(1,11): print(repr(x).rjust(2),repr(x*x).rjust( ...