hdu--4585--lower_bound()

好久不见     lower_bound()

最近 事情太多了 龙兄 来了宁波 我们几个一起陪他玩了几天 那几天就没碰过题了 昨天他回临安了 明年应该就是我们宁波这3个 过去找他了

我们 设计了很多创业大计 描绘了美好的蓝图 就差 资金到位了 哈哈~~

-------

这题的话 就是开始给你一个人的ID与他的能力值  起初这个集合中只有 一个人:Id 1  能力值 1000000000

后来 每次加入一个人的时候 总是要从这个集合中 找出一个人与他进行PK 这个人的能力值要与他是得到这样的关系min( abs(new-old) )因为这是绝对值得 所以如果存在上界与下界同时满足 取下界 即小于<new的值

这样每次插入一个 输出一个 最后就可以了

但是我的代码 运行时间好tm长啊 要900+ms 一共给的时间也就1000ms 我也是 醉了

顺便 提下lower_bound(var)函数是返回一个序列中大于等于var的第一个位置  注意这是一个前闭后开的区间 所以如果这个var大于这个序列的所有元素 那么返回的将是一个没有元素的位置 即xx.end()

 1 #include <iostream>
 2 #include <map>
 3 using namespace std;
 4
 5 map<int,int>mp;
 6
 7 int main()
 8 {
 9     cin.sync_with_stdio(false);
10     int n , Id , grade , var , num;
11     while( cin >> n && n )
12     {
13         mp.clear( );
14         map<int,int>::iterator ans;
15         mp[1000000000] = 1;
16         for( int i = 0 ; i<n ; ++i )
17         {
18             cin >> Id >> grade;
19             if( !i )
20             {
21                 cout << Id << " " << 1 << endl;
22             }
23             else
24             {
25                 ans = mp.lower_bound( grade );
26                 if( ans == mp.begin() )
27                     cout << Id << " " << ans->second << endl;
28                 else
29                 {
30                     var = ans->first;
31                     num = ans->second;
32                     --ans;
33                     if( var-grade < grade-(ans->first) )
34                         cout << Id << " " << num << endl;
35                     else
36                         cout << Id << " " << ans->second << endl;
37                 }
38             }
39             mp[ grade ] = Id;
40         }
41     }
42     return 0;
43 }

today:  

  你说 谈恋爱需要靠什么呢?

  我什么都不想罗列

  最后反正是看脸

  

时间: 2024-08-27 18:18:34

hdu--4585--lower_bound()的相关文章

hdu 4585 Shaolin两种方法(暴力和STL)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 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 mast

HDU 4585

http://acm.hdu.edu.cn/showproblem.php?pid=4585 从原来的人中找出战斗数值最接近的,输出他们两人的序号 要在logn的复杂度完成查找,我用的是set,当然用map也可以,两个内部都是红黑树实现 水题一道 #include <iostream> #include <cstdio> #include <set> #include <cmath> using namespace std ; struct node { i

HDU 4585 Shaolin(STL map)

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 mon

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

Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 3191    Accepted Submission(s): 1350 Problem Description Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shao

hdu 4585 Shaolin两种方法(暴力和STL map set)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 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 mast

[HDU 4585] Shaolin (map应用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4585 题目大意:不停的插入数字,问你跟他相距近的ID号.如果有两个距离相近的话选择小的那个. 用map,先用upper_bound找到大的,然后迭代器减1,就能够找到相近的两个. 然后..用链表不知道为什么有问题.... 而且迭代器it,如果减1的话,不能写 it2 = --it1; 这样会wa 但是..it2 = it1; it2--;这样就AC了,在这里记录一下,今后注意. 1 //#pragm

HDU 4585 Shaolin(map应用+二分)

题目大意:原题链接 初始少林最开始只有一个老和尚,很多人想进少林,每个人有一个武力值,若某个人想进少林,必须先与比他早进去的并且武力值最接近他的和尚比武, 如果接近程度相同则选择武力值比他小的,按照进入少林的先后顺序,求出每个和尚进去的时候应当和哪个和尚比武. #include<map> #include<iostream> using namespace std; int main() { int n,id,g; map<int,int>::iterator it,p

hdu 4585 shaolin 平衡树

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

hdu 4585 项目管理(vector运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4858 ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

hdu 4585 Shaolin(STL map)

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 script