题目

这道题用快排做总是会超时,但是别人的快排就不会超时,最后看博客说最保险的方法还是用哈希的思想【哈希思想:散列再循环,对每一个数字进行通过改变哈希表的地址散列放置,将散列地址的哈希表记为1,这样 当所有数字录入完毕,只需要扫一遍哈希表,不为0的数字已经按照地址大小排好顺序,大大的减少了时间复杂度。】
接下来给出写这道题的代码历程:
【一】:
#include<stdio.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000000];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n,cmp);
for(int i=0;i<m-1;i++)
printf("%d ",a[i]);
printf("%d\n",a[m-1]);
}
return 0;
}

【二】:
#include<iostream>
using namespace std;
#include<algorithm>
int a[1000000];
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
for(int i=n-1;i>=m;i--)
printf("%d ",a[i]);
printf("%d\n",a[m-1]);
}

【三】:



#include <stdio.h>
int a[1000000];
int main(){
int n,m,t;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++){
scanf("%d",&t);
a[500000+t]=1;
}
for(int i=1000001;m>0;i--)
{
if(a[i])
{
printf("%d",i-500000);
if(m>1) printf(" ");
else
printf("\n");
m--;
}
} } return 0;
}


hdu 1425的更多相关文章

  1. hdu 1425 Happy 2004

    题目链接 hdu 1425 Happy 2004 题解 题目大意: 求 \[\sum_{d|2004^{x}}d\ mod\ 29\] 记为\(s(2004^x)\) \(sum(2004^{x})= ...

  2. hdu 1425 sort 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...

  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 1425 sort hash+加速输入

    http://acm.hdu.edu.cn/showproblem.php?pid=1425 题目大意: 给你n个整数,请按从大到小的顺序输出其中前m大的数. 其中n和m都是位于[-500000,50 ...

  5. hdu 1425:sort(排序,经典题。快排模板)

    sort Time Limit : 6000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  6. HDU 1425 sort(堆排序/快排/最大堆/最小堆)

    传送门 Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不 ...

  7. HDU 1425 sort 【哈希入门】

    题意:给出n个数,输出前m大的数 和上一题一样,将输入的数加上一个极大地值作为地址 #include<iostream> #include<cstdio> #include&l ...

  8. HDU 1425 sort 题解

    选择出数列中前k个最大的数. 这里由于数据特殊.所以能够使用hash表的方法: #include <cstdio> #include <algorithm> #include ...

  9. hdu 1425 sort

    Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数.   Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行 ...

  10. HDU 1425 C++使用sort函数

    sort Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...

随机推荐

  1. java 包的命名规范

  2. spring 整合 hibernate xml配置

    spring 整合 hibernate: hibernate :对数据库交互 spring: ioc   aop 整合点: 1.sessionFactory对象不再由hibernate生成,交由spr ...

  3. Ansible 管理MySQL主从复制

    mysql_replication - 管理MySQL复制 1.3版新功能 概要 要求(在执行模块的主机上) 选项 例子 笔记 状态 支持 概要 管理MySQL服务器复制,从属,主状态获取和更改主控主 ...

  4. Honeycomb

    Honeycomb http://codeforces.com/gym/102028/problem/F time limit per test 4.0 s memory limit per test ...

  5. 使用生活实例理解Asp.net运行时

    学习编程语言,掌握面向对象的编程思想尤为重要,一旦理解了面向对象的这种概念,那么好些地方拿到生活中去理解,就容易的多了.书本上的枯燥干涩的语言,对于好多人来说,即难懂,更难长时间牢牢记得.但是编程语言 ...

  6. 53. Maximum Subarray最大求和子数组12 3(dp)

    [抄题]: Find the contiguous subarray within an array (containing at least one number) which has the la ...

  7. OC -网络请求 - NSURLConnection - POST

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  8. 什么是springMvc的参数绑定?

    参数绑定通俗来讲就是从页面传过来的数据通过SpringMvc进行接收.接收的数据类型可以有: (1)SpringMvc默认支持的类型:request.session.application等. (2) ...

  9. html注释快捷键

    1.选中需要注释的内容--->ctrl+shift+/ 2.取消注释--->ctrl+shift+\

  10. 通过程序修改注册表键值来达到修改IE配置参数的目的

    通过程序修改注册表键值来达到修改IE配置参数的目的 使用IE访问应用程序或网页时经常需要设置一些选项(工具-Internet 选项),比如为了避免缓存网页,把工具-Internet选项-常规选项卡-I ...