hdu4585 STL水题

题意:
      成立少林寺,刚开始有一个大师,id是1,攻击力是10E,现在陆续来人,每个人有自己的id,和自己的攻击力,但是每一个新来的要和之前的和尚pk,他必须选择和他攻击力差值最小的那个,如果有两个差值一样的话选择攻击力比他小的那个,输出pk组合..


思路:

     一开始还以为线段树呢,哎! 水题,我用的set找最接近的,map哈希他们的id了,其实直接一个map就行了,自己的STL用的不是很熟,所以开了两个set一个map才AC,总之这个是水题,不多解释..


#include<stdio.h>
#include<set>
#include<map>

using namespace std;
map<int ,int>hash_id;
set<int>st1 ,st2;

int abss(int x)
{
   return x > 0 ? x : -x;
}

int main ()
{
   int n ,i ,a, b;
   while(~scanf("%d" ,&n) && n)
   {
      hash_id.clear();
      st1.clear();
      st2.clear();
      hash_id[1000000000] = 1;
      st1.insert(1000000000);
      st2.insert(-1000000000);
      st2.insert(1);
      for(i = 1 ;i <= n ;i ++)
      {
         scanf("%d %d" ,&a ,&b);
         int aa = *st1.lower_bound(b);
         int bb = *st2.lower_bound(-b) * -1;
         if(bb < 0)
         printf("%d %d
" ,a ,hash_id[aa]);
         else
         {
            if(abss(b - bb) <= abss(b - aa))
            printf("%d %d
" ,a ,hash_id[bb]);
            else
            printf("%d %d
" ,a ,hash_id[aa]);
         }
         st1.insert(b);
         st2.insert(-b);
         hash_id[b] = a;
      }
   }
   return 0;
}

原文地址:https://www.cnblogs.com/csnd/p/12063207.html