HDU 4585 Shaolin (STL map)

Source: Internet
Author: User
Tags abs unique id

Shaolin Time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u Submit 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 he talent on understanding the Buddism scripture, but fighting Skil L is also taken to account.
When a young mans passes all the tests and is declared a new monk of Shaolin, there'll be a fight, as a part of the WELC Ome party. Every monk has a unique ID and a unique fighting grade, which is all integers. The new monk must fight with an old monk whose fighting grade are closest to his fighting grade. If there is a monks satisfying that condition, the new monk would take the one whose fighting grade was less than his .
The Master is the first monk-Shaolin, his ID is 1,and he fighting grade is 1,000,000,000.he just lost the fighting rec Ords. But he still remembers who joined Shaolin earlier, who joined later. Recover the fighting records for him.

Input There is several test cases.
In each test case:
The first line was 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 an integer k and G, meaning a monk ' s ID and his fighting grade. (0<= K, g<=5,000,000)
The monks is listed by ascending order of jointing time. In the other words, monks who joined Shaolin earlier come first.
The input ends with n = 0.

Output A fight can be described as both 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 and then the old monk's ID.

Sample Input

3 2 1 3 3 4 2 0

Sample Output

2 1 3 2 4 2



Roughly test instructions: a long time ago, Shaolin Temple only a grade 10^9 for the abbot, he numbered 1, Shaolin Temple there is a rule, each new monk must and a rank closest to his old (that is, than he came first) monk fighting, and then came to n monks, and they press into the temple time numbered 2~n+ 1, but the time is too long, the abbot has no idea when each monk came to fight with which monk, but he still remember which monk than which and Chanchenlay. The input gives the number of n monks and their rank. Below you, let you find out in turn each monk into the temple is who fight, and output.



Problem-solving ideas: Directly open the array, every time to find the most close to his rank is sure to sort, now know the fastest fast platoon is also O (n*lg N), because the data to the 10^5, coupled with the operation, the time has been super. So, you have to change the idea, it will have to use the advantages of the map of the STL, but also for a long time no map to do the problem. The data in the map will automatically be sorted in ascending order, it is said that the order of the map is implemented with red and black trees (an unknown data structure), its time complexity is O (n), and the map can also be directly used to find the key value of the find () keys where the iterator, which is too convenient.

Implementation steps: 1. First open a map<int, Int> monk, the abbot's number ID and level grad in the map to build a map, here to use a small detail,

The build map can be written directly like this, monk[grad] = ID.

2. Enter the information of n monks in a loop,

1) Each time you enter a monk's information, and then set up a map;

2) use Find (grad) now to find the iterator where the monk is currently joined;

3) Then, consider the position of the current iterator, and if the last one, directly outputs the previous one of the current iterator;

If the first one, output the following one of the current iterators;

To be neither the first nor the last, compare the difference between the current and the front and back of the iterator, which is smaller, smaller output;

If the two values 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&
Gt
#include <stdlib.h> #include <time.h> using namespace std;      #define INF 0x7fffffff Map<int, int> A;
    Declares 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;
            Handle the abbot for (int i=0; i<n; i++) {scanf ("%d%d", &k, &g);
            A[g] = k;      Map<int, Int>::iterator temp = a.find (g);
            Press Grad to find the current monk's iterator 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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.