zoukankan      html  css  js  c++  java
  • HDU 4585 Shaolin (STL)

    Shaolin

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 315    Accepted Submission(s): 162


    Problem 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
     
    Source
     

    函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置

    举例如下:

    一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标

    pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。

    pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。

    pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。

    所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~

    返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<set>
    #include<map>
    
    using namespace std;
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n,k,g;
        set<int> st;
        map<int,int> mp;
        while(~scanf("%d",&n) && n){
            st.clear();
            mp.clear();
            st.insert(1000000000);
            mp[1000000000]=1;
            while(n--){
                scanf("%d%d",&k,&g);
                printf("%d ",k);
                set<int>::iterator it=st.lower_bound(g);
                if(it==st.end()){
                    it--;
                    printf("%d
    ",mp[(*it)]);
                }else{
                    int tmp=(*it);
                    if(it!=st.begin()){
                        it--;
                        if(g-(*it)<=tmp-g)
                            printf("%d
    ",mp[(*it)]);
                        else
                            printf("%d
    ",mp[tmp]);
                    }else
                        printf("%d
    ",mp[(*it)]);
                }
                mp[g]=k;
                st.insert(g);
            }
        }
        return 0;
    }
  • 相关阅读:
    Windows 2008 server + IIS 7 设置身份模拟(ASP.NET impersonation)
    记录windows操作系统启动日志
    C# 重启计算机的问题
    AcWing 1086 恨7不是妻
    AcWing 1084. 数字游戏 II
    AcWing 1083. Windy数
    golang学习笔记 生成JSON及解析JSON 清明
    WEB前端底层知识之浏览器是如何工作的(2)渲染引擎
    ASP.NET MVC Razor 输出没有编码的HTML字符串
    ext.grid的配置属性和方法
  • 原文地址:https://www.cnblogs.com/jackge/p/3251016.html
Copyright © 2011-2022 走看看