HDU1425 sort【水题】
来源:互联网 发布:不可使知之 编辑:程序博客网 时间:2024/05/16 10:46
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1425
题目大意:
给你N和M,然后给你N个数,输出前M大的数。
思路:
数据范围为-500000~500000,N的范围为1000000。两种做法。
1).建个1000000数组,存N个数,用sort排下序,输出前M大的数,这样提交900+MS,有时会超时。
2).建个1000000数组,用下标表示数值,输入一个数,就将对应下标的数值加一。最后从后向前遍历,
找到M个数并输出。
AC代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;int Num[1000010];int 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",&Num[i]); sort(Num,Num+N,cmp); for(int i = 0; i < M; ++i) if(i != M-1) printf("%d ",Num[i]); else printf("%d\n",Num[i]); } return 0;}
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;int Num[1000010];int main(){ int N,M,d; while(~scanf("%d%d",&N,&M)) { memset(Num,0,sizeof(Num)); for(int i = 0; i < N; ++i) { scanf("%d",&d); Num[d+500000]++; } int Cnt = 0; for(int i = 1000000; i >= 0; --i) { while(Num[i]) { Num[i]--; Cnt++; if(Cnt != M) printf("%d ",i-500000); else { printf("%d\n",i-500000); } } if(Cnt == M) break; } } return 0;}
0 0
- HDU1425 sort【水题】
- hdu1425 sort
- HDU1425:sort
- HDU1425-sort
- HDU1425----sort
- hdu1425.sort
- HDU1425:sort
- HDU1425 sort
- HDU1425-sort
- HDU1425 sort
- hdu1425 sort (hash)
- hdu1425 sort 解题报告
- sort HDU1425 TLE?!
- hdu1425——sort
- hdu1425 sort (朴素)
- hdu1425 sort(哈希表)
- HDU1425 <sort 快排>
- HDU1425 sort 【STL堆排序】
- PHP资源类型
- 表达式树
- The 12th Zhejiang Provincial Collegiate Programming Contest - D
- 面向对象(子父类中构造函数的特点-子类实例化过程)
- 读书笔记---vi编辑命令解析
- HDU1425 sort【水题】
- java web.xml 基本配置了解(1)
- UINavigationController导航栏传值、改变颜色
- 解决问题:maven3.3.1 的-Dmaven.multiModuleProjectDirectory system propery is not set
- mac常用快捷键
- zoj 3880 Demacia of the Ancients
- gcd() 最大公约数
- 编程题目C++字符串相关
- ubuntu14.04编译Android4.4