杭电1800Flying to the Mars(简单哈希)
来源:互联网 发布:华为服务器安装linux 编辑:程序博客网 时间:2024/05/27 21:02
->题目请戳这里<-
题目大意:给n个数,表示n个人的等级,现在这n个人要去攻占火星,要坐扫把去,能坐在同一个扫把上的人要满足他们的等级严格递减,求最少的扫把数。
题目分析:就是求一个数字串的单调递减(递增)的子序列数,也就是求出现次数最多的那个数。给定数字范围是2^30,所以用哈希做。哈希函数定义的是每个数字各位平方和,跑了四百多毫秒,效率不是很高。。。
详情请见代码:
#include <iostream>#include<cstdio>#include<cstring>using namespace std;struct node{ int key,num;}hash[6005];int p = 2999;int getkey(int x){ int k = 0; int t; while(x) { t = x % 10; k += t * t; x /= 10; } return k % p;}int main(){ int i; int n,m; int key; while(scanf("%d",&n) != EOF) { memset(hash,-1,sizeof(hash)); int mx = 0; int ans = 1; for(i = 0;i < n;i ++) { scanf("%d",&m); key = getkey(m); if(key > mx) mx = key; if(hash[key].key == -1) { hash[key].key = m; hash[key].num = 1; } else { int pos = key; while(hash[pos].key != -1) { if(hash[pos].key == m) break; pos ++; } if(hash[pos].key == m) hash[pos].num ++; else { hash[pos].key = m; hash[pos].num = 1; } if(mx < pos) mx = pos; if(ans < hash[pos].num) ans = hash[pos].num; } } printf("%d\n",ans); } return 0;}//421MS340K
- 杭电1800Flying to the Mars(简单哈希)
- 杭电1800-Flying to the Mars
- 杭电1800Flying to the Mars
- 杭电 1800 Flying to the Mars(贪心)
- 杭电1800 Flying to the Mars(贪心)
- 杭电 HDU ACM 1800 Flying to the Mars
- 杭电 hdu 1800 Flying to the Mars (水题 STL)
- hdoj 1800 Flying to the Mars (字符串哈希函数)
- 1800 Flying to the Mars
- hdu 1800 Flying to the Mars(map简单题)
- (map简单使用) Flying to the Mars
- Flying to the Mars
- Flying to the Mars
- Flying to the Mars
- Flying to the Mars
- Flying to the Mars
- Flying to the Mars
- Flying to the Mars
- oracle中Where子句的条件顺序对性能的影响 .
- dumpsys
- Find the Number Occurring Odd Number of Times
- Js获取当前日期时间及其它操作
- Find the two non-repeating elements in an array of repeating elements
- 杭电1800Flying to the Mars(简单哈希)
- 消息软删除
- Bison-Flex 笔记
- Java学习笔记45:Java 线程与主线程之间的变量关系
- 关于Ovirt的TUI库
- uva 264Count on Cantor
- Rotate bits of a number
- extjs 和 jquery区别
- CE with requiem