杭电1800Flying to the Mars(简单哈希)
2013-06-08 12:21
337 查看
->题目请戳这里<-
题目大意:给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;
}
//421MS 340K
题目大意:给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;
}
//421MS 340K
相关文章推荐
- 杭电1800 Flying to the Mars(贪心)
- 杭电1800Flying to the Mars
- HDU 1800 Flying to the Mars 字符串哈希||字典树||map
- 杭电 1800 Flying to the Mars(贪心)
- 杭电 HDU ACM 1800 Flying to the Mars
- HDU 1800——Flying to the Mars——————【字符串哈希】
- hdu 1800 Flying to the Mars(简单模拟,string,字符串)
- hdu 1800 Flying to the Mars 详细题解 哈希
- HDU - 1800 Flying to the Mars(桶排|sort|哈希|map)
- 杭电1800-Flying to the Mars
- hdu 1800 Flying to the Mars(map简单题)
- 杭电 1800 Flying to the Mars(贪心)
- hdu---(1800)Flying to the Mars(trie树)
- HDU 1800 Flying to the Mars 字符串 hash
- hdoj 1800 Flying to the Mars 【贪心】
- hdu 1800 Flying to the Mars
- F - Flying to the Mars HDU - 1800
- HDOJ.1800 Flying to the Mars(贪心+map)
- CSU-ACM2017暑期训练10-并查集&&HASH F - Flying to the Mars HDU - 1800(字符串hash)
- 1800 Flying to the Mars