Shaolin
Time limit:1000 ms
Memory limit:32768kb
64bit Io format:% I64d & % i64usubmit status practice HDU 4585
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 supported DED ). 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
32 13 34 20
Sample output
2 13 24 2
A long time ago, the Shaolin Temple had only a grade of 10 ^ 9 as the abbot. He was numbered 1 and there was a rule in the Shaolin Temple, each new monk and Shang must fight with an old monk whose rank is closest to him (that is, the monk who comes first). Then there are n New monks, and they are numbered 2 ~ N + 1, but the time was too long. The abbot had no idea which monk he was fighting with when he was still here, but he still remembered which monk came first than which one. Enter the number and level of the N monks. Next we will rely on you to find out in sequence who each monk fought in the temple and output it.
Solution: If you open an array directly, you must sort the data that is closest to the other level. Currently, the fastest fast sorting you know is O (n * lg n, because the data is 10 ^ 5, and the operation is required, the time has exceeded. So you have to change your mind, so you have to use the advantages of map in STL, and it has not been used for map for a long time. The data in map is automatically sorted in ascending order by keys. It is said that the internal sorting of map is implemented by the red/black tree (an unknown data structure, the time complexity is O (n). At the same time, you can use the find () key value in the map to find the iterator where the key value is located, which is too convenient.
Specific implementation steps: 1. First open a Map <int, int> monk, and map the ID and level grad of the abbot in the map. Here we use a small detail,
You can write the moning directly in this way. MONK [grad] = ID.
2. Input n monk information in a loop,
1) enter the information of a monk each time, and then create a ing in map;
2) Use Find (GRAD) to find the iterator where the current monk is located;
3) then, consider the current iterator position. If it is the last one, the previous one of the current iterator will be output directly;
If the first iterator is used, the next iterator of the current iterator is output;
If it is neither the first nor the last one, the difference between the first and the first of the current iterator is smaller and smaller;
If the two differences are the same, the previous iterator is output.
AC code:
# 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 0x7fffffmap <int, int> A; // declare a map object int main () {# ifdef sxk freopen ("in.txt ", "r", stdin); # endif int N, K, G; while (scanf ("% d", & N )! = EOF & N) {A [1e9] = 1; // processing Abbot for (INT I = 0; I <n; I ++) {scanf ("% d", & K, & G); A [g] = K; Map <int, int >:: iterator temp =. find (g); // search for the iterator where the current monk is located by grad if (temp =. begin () printf ("% d \ n", K, (++ temp)-> second); else if (temp =. end () printf ("% d \ n", K, (-- temp)-> second); else {If (ABS (-- temp) -> first-(++ temp)-> first)> ABS (++ temp)-> first-(-- temp)-> first )) printf ("% d \ n", K, (++ temp)-> second); else printf ("% d \ n", K, (-- temp)-> second) ;}}. clear () ;}return 0 ;}
HDU 4585 Shaolin (STL map)