您的位置:首页 > 其它

HDOJ HDU 1029 Ignatius and the Princess IV

2017-09-06 22:30 417 查看

HDOJ 1029 Ignatius and the Princess IV

题目

点此查看 HDOJ 1029 Ignatius and the Princess IV

题意

有于 N 最大为 999999

用map记录是一种方法

效率也比较高1.

由于数列中会出现大量相邻数字

我的做法是 记录 前后两位相减为 0 的数 找出最大的输出即可

技巧

本题中 输入量非常大

我多次出现TLE

看到高手的做法

加入了 ios::sync_with_stdio(0);

加上这一句将scanf改为cin流式输入反而快,160多ms就AC了

我认为是cin的输入流缓冲区批量读取大量元素,结果反而比scanf读取快

代码

//由于有很多重复的数,这些数前后相减必为零,找出为零最多的数则必为解
#include <iostream>
#include <map>

using namespace std;

int main()
{
ios::sync_with_stdio(0);
//一直TLE,加上这一句将scanf改为cin流式输入反而快了160多ms就AC了
//我认为是cin的输入流缓冲区批量读取大量元素,结果反而比scanf读取快
int n,sp,lst,crt,ans,maxnum;
map<int,int> m;
while(cin >> n)
{
sp = (n+1)/2;
cin >> lst;
for(int i = 1;i < n;i++)
{
cin >> crt;
if(crt - lst == 0)
{
//              cout << "cur " << crt << endl;
if(!m.count(crt))
m[crt] = 1;
else m[crt] ++;
}
lst = crt;
}
ans = maxnum = 0;
for(map<int,int>::iterator it = m.begin();it != m.end();it++)
{
if(it->second > maxnum)
{
ans = it->first;
maxnum = it->second;
}
}
cout << ans << endl;
m.clear();
}
return 0;
}


C++ STL 模板 中的 map 底层的数据结构为 红黑树.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDOJ