Description

Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture,
but fighting skill is also taken into account. 

When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose
fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his. 

The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him. 
 

Input

There are several test cases. 

In each test case: 

The first line is a integer n (0 <n <=100,000),meaning the number of monks who joined Shaolin after the master did.(The master is not included).Then n lines follow. Each line has two integer k and g, meaning a monk's id and his fighting grade.( 0<= k ,g<=5,000,000) 

The monks are listed by ascending order of jointing time.In other words, monks who joined Shaolin earlier come first. 

The input ends with n = 0. 
 

Output

A fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk's id first ,then the old
monk's id.
 

Sample Input

3
2 1
3 3
4 2
0
 

Sample Output

2 1
3 2
4 2

大致题意:非常久曾经。少林寺里仅仅有一个等级10^9为方丈,他编号为1,少林寺里有个规矩,每一个新来的和尚都必须和一位等级最接近自己的老(就是比他先来的)和尚进行战斗。后来又来了n个和尚。而且他们按入寺时间依次编号2~n+1,可是时间太久。方丈已经不知道每一个和尚来的时候是跟哪个和尚战斗了,可是他还是记得哪个和尚比哪个和尚先来的。

输入给出n个和尚的编号和他们的等级。

以下就靠你了,让你依次求出每一个和尚进寺是是跟谁战斗的,并输出。

解题思路:直接开数组的话,每次找跟他等级最接近的肯定要排序。眼下知道的最快的快排的也是O(n*lg n)的,因为数据给的是10^5,再加上还要操作,时间已经超了。

所以,得换个思路,那就得用到STL中的map的优势了,也好久没用map做题了。map里的数据会自己主动按键值升序排列。据说map内部的排序是用红黑树(一种不明觉厉的数据结构)实现的。其时间复杂度是O(n)的。同一时候,map里还能够直接用find()按键值查找键值所在的迭代器,这就太方便了。

详细实现步骤:1.先开个map<int, int> monk,  把方丈的编号id和等级grad在map里建立映射。这里用到了一个小细节。

建立映射能够直接这样写。monk[grad] = id.

2.在循环输入n个和尚的信息,

1)每次输入一个和尚的信息。然后在map里建立映射;

2)此时用find(grad)寻找当前增加的和尚所在的迭代器。

3)然后,考虑当前的迭代器的位置,若是最后一个。就直接输出当前迭代器的前一个;

要是第一个。就输出当前迭代器的后面一个;

要既不是第一个也不是最后一个的话。就比較当前迭代器的first和它前后两迭代器first之间的差值哪个更小,小的输出;

要是两差值一样的话,输出前一个迭代器。

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff map<int, int> a; //声明一个map对象 int main()
{
#ifdef sxk
freopen("in.txt","r",stdin);
#endif
int n, k, g;
while(scanf("%d",&n)!=EOF && n)
{
a[1e9] = 1; //处理方丈
for(int i=0; i<n; i++){
scanf("%d%d", &k, &g);
a[g] = k;
map<int, int>::iterator temp = a.find(g); //按grad查找当前和尚所在的迭代器
if(temp == a.begin()) printf("%d %d\n", k, (++temp)->second);
else if(temp == a.end()) printf("%d %d\n", k, (--temp)->second);
else{
if(abs((--temp)->first - (++temp)->first) > abs((++temp)->first - (--temp)->first))
printf("%d %d\n", k, (++temp)->second);
else printf("%d %d\n", k, (--temp)->second);
}
}
a.clear();
}
return 0;
}

HDU 4585 Shaolin(STL map)的更多相关文章

  1. HDU 4585 Shaolin (STL map)

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

  2. HDU 4585 Shaolin (STL)

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

  3. HDU 1263 水果 (STL map)

    水果 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

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

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

  5. HDU 4585 Shaolin(水题,STL)

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

  6. HDU 2094 产生冠军(STL map)

    产生冠军 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. hdu 4858 项目管理(STL集装箱)

    项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. CodeForces 501B Misha and Changing Handles(STL map)

    Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user ...

  9. 【UVA】10391 Compound Words(STL map)

    题目 题目     分析 自认已经很简洁了,虽说牺牲了一些效率     代码 #include <bits/stdc++.h> using namespace std; set <s ...

随机推荐

  1. [luogu] P3089 [USACO13NOV]POGO的牛Pogo-Cow

    P3089 [USACO13NOV]POGO的牛Pogo-Cow 题目描述 In an ill-conceived attempt to enhance the mobility of his pri ...

  2. 【hihocoder 1519】 逃离迷宫II

    [题目链接]:http://hihocoder.com/problemset/problem/1519?sid=1098756 [题意] Chinese [题解] bfs题; 根据bfs的性质; 第一 ...

  3. 今天修了一个bug,关于debug日志的问题

    是别人的代码,很诡异. 就是开了debug日志,没问题. 关了debug日志,就出问题. 开始我以为是debug日志拖慢了速度,所以有一些竞态环境的影响. 后来发现是在debug日志里面有一些side ...

  4. ZOJ 3435

    求(1,1,1)至(x,y,z)的互质个数. 即求(0,0,0)到(x-1,y-1,z-1)互质个数. 依然如上题那样做.但很慢...好像还有一个分块的思想,得学学. #include <ios ...

  5. 少年 DXH

    少年 DXH 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 大家都知道,DXH 幼时性格怪癖,小朋友都不喜欢和他玩,这样的情况一直到 DXH 的少年时期也没有改变. ...

  6. pcap网络抓包 无法import pcap

    坑爹的不知道从哪里看到说仅仅有pcap最多仅仅支持到python2.5,然后又是easy install又是安装pip就是无法成功import pcap... 我的python版本号是2.7.8. s ...

  7. JBoss AS 7之文件夹结构(The Return Of The King)

    1.2 JBoss As 7体系结构 以下介绍一下JBoss的体系结构,详细的文件夹结构. 假设熟悉曾经JBoss版本号的人,一定会发现JBoss AS 7与之前的JBoss的文件夹结构有了非常大的不 ...

  8. Android开发之AudioManager(音频管理器)具体解释

    AudioManager简单介绍: AudioManager类提供了訪问音量和振铃器mode控制. 使用Context.getSystemService(Context.AUDIO_SERVICE)来 ...

  9. 解决Myeclipse在调试(debug)时无法显示变量值问题

    解决Myeclipse在调试(debug)时无法显示变量值问题 突然发现myeclipse在调试时当鼠标放在变量上面时无法显示变量值了 ctrl+shift+D居然提示cannot be resolv ...

  10. QMutex“A mutex must be unlocked in the same thread that locked it”解决(在run里创建对象是不二法宝)

    多线程时出现如下警告信息: A mutex must be unlocked in the same thread that locked it: 原因可能有二: 1.创建QMutex不在当前线程: ...