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 底层的数据结构为 红黑树. ↩
相关文章推荐
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- dp-基础3 B - Ignatius and the Princess IV HDU - 1029
- 【HDU】 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV【贪心】
- hdu-oj 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV(快排)
- hdu 1029 Ignatius and the Princess IV(水题)
- HDU 1029 Ignatius and the Princess IV 这题有点蛋疼
- HDU - 1029 Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV 动态规划
- 【hdu 1029】Ignatius and the Princess IV
- HDOJ 1029 Ignatius and the Princess IV
- HDU 1029 - Ignatius and the Princess IV
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV 【求众数】
- hdu 1029 Ignatius and the Princess IV
- HDU 1029 Ignatius and the Princess IV