[HDU 4585] Shaolin (map应用)
2014-11-23 12:36
435 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585
题目大意:不停的插入数字,问你跟他相距近的ID号。如果有两个距离相近的话选择小的那个。
用map,先用upper_bound找到大的,然后迭代器减1,就能够找到相近的两个。
然后。。用链表不知道为什么有问题。。。。
而且迭代器it,如果减1的话,不能写 it2 = --it1; 这样会wa
但是。。it2 = it1; it2--;这样就AC了,在这里记录一下,今后注意。
题目大意:不停的插入数字,问你跟他相距近的ID号。如果有两个距离相近的话选择小的那个。
用map,先用upper_bound找到大的,然后迭代器减1,就能够找到相近的两个。
然后。。用链表不知道为什么有问题。。。。
而且迭代器it,如果减1的话,不能写 it2 = --it1; 这样会wa
但是。。it2 = it1; it2--;这样就AC了,在这里记录一下,今后注意。
//#pragma comment( linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring> #include <queue> #include <algorithm> #include <cmath> #include <map> #include <iterator> using namespace std; typedef map<int,int>::iterator pt; map<int,int> mp; int n; int main(){ while(scanf("%d",&n),n){ int k,g; mp.clear(); mp.insert(make_pair(1000000000,1)); for(int i=0;i<n;i++){ scanf("%d%d",&k,&g); pt it2 = mp.upper_bound(g); if( it2==mp.begin() ){ printf("%d %d\n",k,it2->second); mp.insert(make_pair(g,k)); continue; } pt it1 = it2; it1--; if( abs(g-it1->first)<=abs(g-it2->first) ) { printf("%d %d\n",k,it1->second); } else { printf("%d %d\n",k,it2->second); } mp.insert(make_pair(g,k)); } } return 0; }
相关文章推荐
- hdu 4585 shaolin—— STL lower_bound的应用
- HDU 4585 Shaolin (map)
- hdu 2095 (find your present (2))基础题 map的应用
- hdu 4585 2013杭州邀请赛 Shaolin
- hdu 4585 Shaolin
- hdu 4585 Shaolin_set用法
- hdu 1075 map 的应用可以字符串的hash
- hdu 2072 单词数(map的简单应用)
- HDU 4585 Shaolin(听说兼职和尚100+/天)
- HDU 1004 Let the Balloon Rise Map的应用
- HDU 4585 Shaolin (STL)
- hdu 2648(map简单应用)
- HDU 4302 MAP应用
- hdu 4585 Shaolin 2013 ACM-ICPC杭州赛区全国邀请赛——题目重现
- HDU 4585 Shaolin
- hdu 4585 Shaolin
- hdu 1381 Crazy Search(hash/map的应用)
- hdu 4022map和list等数据结构的应用
- hdu-4585-Shaolin 平衡二叉树
- hdu 4585 Shaolin(二分)