HDU1800 用map会超时 请教解决办法

sunyuke 2009-02-26 08:59:10
1800 Flying to the Mars
中文大意如下:PPF要攻占火星 有n个(小于3000)士兵 各有一个level (less than 30 digits)
现在他们要做飞天扫把去火星 但是要培训 level值大的士兵后面可以坐小的 每把扫把上可以坐无限个人 但是要保证他们的level是递减的,
求所用最小扫把数

一下是我的代码 但是老超时 请问原因 如果有不同的解法 请尽量写出代码 谢谢!


#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
int n;
while(cin>>n)
{
if(n == 0)
break;
map<string ,int> m;
map<string ,int>::iterator index;
string input;
for(int i = 0; i <n;i++)
{
cin>>input;
m[input]++;
}
int max = -1;
for(index=m.begin();index!=m.end();index++)
{
int temp = index->second;
if(temp > max)
max = temp;
}
cout<<max<<endl;
m.clear();
}
return 0;
}
...全文
240 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
竹二木 2010-08-07
  • 打赏
  • 举报
回复
等级数不是达到了30个数字吗?你两个int型居然过了 ????、
fremn 2010-08-07
  • 打赏
  • 举报
回复
http://blog.csdn.net/Jsjdream/archive/2010/07/12/5727775.aspx这个的咋没超时
komai908 2009-10-18
  • 打赏
  • 举报
回复
great!
绿色夹克衫 2009-02-27
  • 打赏
  • 举报
回复
定义一个长度为30的数组,分别记录level0-level29的士兵人数,然后输出这30个数中的最大值应该就可以吧?
sunyuke 2009-02-27
  • 打赏
  • 举报
回复
大家好 我已经解决这个问题了 谢谢大家的关注
用cin输入是用到流对象 所以改成scanf比较好
还有<string,int>这里的string构造时间比较多 所以改成<int,int>
这样提交就通过了
以下是代码

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int main ()
{
int n;
while(cin>>n)
{
if(n == 0)
break;
map<int,int> m;
map<int,int>::iterator index;
int input;
for(int i = 0; i <n;i++)
{
scanf("%d",&input);
m[input]++;
}
int max = -1;
for(index=m.begin();index!=m.end();index++)
{
int temp = index->second;
if(temp > max)
max = temp;
}
cout<<max<<endl;
m.clear();
}
return 0;
}
sunnywyg 2009-02-26
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main ()
{
int n;
while(cin>>n)
{
if(n == 0)
break;
map<int,int> m;
map<int,int>::iterator index;
int input;
int max = -1;
for(int i = 0; i <n;i++)
{
cin>>input;
m[input]++;
max = max > m[input] ? max : m[input];
}
cout<<max<<endl;
m.clear();
}
return 0;
}


不过这样也超时..
waizqfor 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sunyuke 的回复:]
引用 1 楼 ayw215 的回复:
C/C++ codefor(index=m.begin();index!=m.end();index++)
{
int temp = index->second;
if(temp > max)
max = temp;
}


干嘛要自己写遍立?
直接用map里面的find不是更快?

用find怎么找到那个次数最大的项呢?
可以的话 写出代码好么
[/Quote]
find 能找到最大值 不是吧 我记得只能找到关键字啊
  • 打赏
  • 举报
回复
find只能按值匹配,不能找到最大值。
dongpy 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ayw215 的回复:]
C/C++ codefor(index=m.begin();index!=m.end();index++)
{
int temp = index->second;
if(temp > max)
max = temp;
}



干嘛要自己写遍立?
直接用map里面的find不是更快?
[/Quote]
楼主的算法没问题。
find只能按关键字查找,不能得到最大值。
sunyuke 2009-02-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ayw215 的回复:]
C/C++ codefor(index=m.begin();index!=m.end();index++)
{
int temp = index->second;
if(temp > max)
max = temp;
}



干嘛要自己写遍立?
直接用map里面的find不是更快?
[/Quote]
用find怎么找到那个次数最大的项呢?
可以的话 写出代码好么
ayw215 2009-02-26
  • 打赏
  • 举报
回复
for(index=m.begin();index!=m.end();index++)
{
int temp = index->second;
if(temp > max)
max = temp;
}

干嘛要自己写遍立?
直接用map里面的find不是更快?

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧