hdu acm 1425 sort(哈希表思想)
sort
Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25803 Accepted Submission(s): 7764
Hint
请用VC/VC++提交
最简单的运用了哈希表的思想的题,开始看了好多关于散列的理论资料,太深奥反而抓不住基本思想。
基本做法
#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(哈希表思想)的更多相关文章
- HDU 1496 Equations(哈希表)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1496 [题目大意] 给出一个方程ax1^2+bx2^2+cx3^2+dx4^2=0,求-100到1 ...
- 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 ...
- E题hdu 1425 sort
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 sort Time Limit: 6000/1000 MS (Java/Others) M ...
- hdu 5183. Negative and Positive (哈希表)
Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU4887_Endless Punishment_BSGS+矩阵快速幂+哈希表
2014多校第一题,当时几百个人交没人过,我也暴力交了几发,果然不行. 比完了去学习了BSGS才懂! 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4887 ...
- 【哈希表】CodeVs1230元素查找
一.写在前面 哈希表(Hash Table),又称散列表,是一种可以快速处理插入和查询操作的数据结构.哈希表体现着函数映射的思想,它将数据与其存储位置通过某种函数联系起来,其在查询时的高效性也体现在这 ...
- 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 ...
- [转]net中哈希表的使用 Hashtable
本文转自:http://www.cnblogs.com/gsk99/archive/2011/08/28/2155988.html 以下是PetShop中DBHelper中的使用过程: //创建哈希表 ...
- Snowflake Snow Snowflakes(哈希表的应用)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 27312 Accep ...
随机推荐
- photoshop cs5 key
序列号: 1330-1164-2870-9234-4243-7879 1330-1027-8489-4513-0233-4890 1330-1176-2865-0373-1551-0175 1330- ...
- linux学习笔记--文件
文件系统 ls -lhi i inode 相当于文件在磁盘里的唯一标示,index node h 代表文件大小k,m [root@masters ~]# [root@masters ~]# ls -l ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中web相关的知识(概述)
Spring Framework中web相关的知识 1.概述: 参考资料:官网documentation中第22小节内容 关于spring web mvc: spring framework中拥有自 ...
- Android 7.0 UICC 分析(二)
本文讲解UiccCard类 /frameworks/opt/telephony/src/java/com/android/internal/telephony/uicc/UiccCard.java U ...
- 2016CCPC 合肥--最大公约数//每一年通向它的路上,多少人折戟沉沙,多少人功败垂成,有人一战成名,有人从头再来。
有这样一个有关最大公约数的函数:函数 f(x, y): { c=0 当 y>0: { c +=1 t = x % y x = y y = t } 返回 c * x * x} 给出三个正整数n,m ...
- 用 python实现简单EXCEL数据统计
任务: 用python时间简单的统计任务-统计男性和女性分别有多少人. 用到的物料:xlrd 它的作用-读取excel表数据 代码: import xlrd workbook = xlrd.open_ ...
- ext.ajax.request请求时带有遮罩效果
ajax请求时有时需要操作大量的数据,反应有时会很慢,这时我们想要来一个遮罩效果,具体步骤如下 1.定义一个遮罩 var myMask = new Ext.LoadMask(Ext.getBody() ...
- 保护眼睛,把常用软件的背景设置成Dark
每天长时间使用电脑,很多软件的背景都是白色,久看对眼睛不好. 1)Google Chrome,WebDev/看新闻/看邮件/写博客.使用Stylish插件和Global Dark Style,效果相当 ...
- error C2065: “CDatabase”: 未声明的标识符
使用vc++与access的接口时出现错误“error C2065: “CDatabase”: 未声明的标识符” 解决方法: 添加 #include "afxdb.h"
- webservice jsonp格式调用
前端 $.ajax({ type: "get", url: "http://baiduzd.yihu.com.cn/APIS ...