题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585

题目大意:不停的插入数字,问你跟他相距近的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(,));
for(int i=;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 ;
}

[HDU 4585] Shaolin (map应用)的更多相关文章

  1. HDU 4585 Shaolin(map应用+二分)

    题目大意:原题链接 初始少林最开始只有一个老和尚,很多人想进少林,每个人有一个武力值,若某个人想进少林,必须先与比他早进去的并且武力值最接近他的和尚比武, 如果接近程度相同则选择武力值比他小的,按照进 ...

  2. HDU 4585 Shaolin(STL map)

    Shaolin Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit cid= ...

  3. HDU 4585 Shaolin(Treap找前驱和后继)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Su ...

  4. hdu 4585 Shaolin(STL map)

    Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao ...

  5. HDU 4585 Shaolin (STL map)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  6. A -- HDU 4585 Shaolin

    Shaolin Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java clas ...

  7. hdu 4585 map **

    题意: Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every ...

  8. HDU 4585 Shaolin (STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  9. HDU 4585 Shaolin(水题,STL)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

随机推荐

  1. PHP 5.6.11 访问SQL Server2008R2

    PHP天生支持MySQL,但是有时候也想让它访问SQL Server,该怎么办呢? 最近找了点资料,测试成功了PHP访问SQLSvr的几种情况,限于时间,还没有测试更多不同环境,把测试过的记录如下: ...

  2. C语言中的数组和指针以及字符串

    数组名同时也是该数组首元素的地址,而指针提供了一种用来使用地址的符号方法,因此指针能够很有效地处理数组. 将一个整数加给指针,这个整数会和指针所指类型的字节数相乘,然后所得的结果会加到初始地址上 da ...

  3. [mysql] mysql explain 使用

    explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 先解析一条sql语句,看出现什么内容 EXPLAINSELECTs.uid, ...

  4. 查看.Net Framework版本的方法

    乐博网最新补充(乐博网一步步教你如何最快查看本机.net framework的版本): 方法一: 第一步: 打开“我的电脑“,在地址栏输入  %systemroot%\Microsoft.NET\Fr ...

  5. LintCode "Digit Counts" !!

    Lesson learnt: one effective solution for bit\digit counting problems: counting by digit\bit http:// ...

  6. (转) 在Eclipse中进行C/C++开发的配置方法(20140721最新版)

    本文转载自:http://blog.csdn.net/baimafujinji/article/details/38026421 Eclipse 是一个开放源代码的.基于Java的可扩展开发平台.就其 ...

  7. 关于学习Perl

    Perl是一门很有用的语言,可以用它来做很多事.然而,它也仅是一门语言,掌握了Perl,你只是掌握了Computer领域的一小块知识.在学习Perl前,请明确你的学习目的,并采用正确的学习方法和资源. ...

  8. 原生视觉差滚动---js+css;

    <!doctype html> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 黄聪:Mysql5.6缓存命中率

    MySQL缓存命中率,网上说法不一,下面我说下我的看法,大家轻拍: 总的select查询数等于com_select(没命中) + qcache_hits(命中) + 解析错误的查询. 再来看看Com_ ...

  10. 配置文件——WebApp.config文件读取和修改

    using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using ...