码迷,mamicode.com
首页 > 其他好文 > 详细

冷血格斗场

时间:2017-02-10 01:34:01      阅读:331      评论:0      收藏:0      [点我收藏+]

标签:bsp   strong   pair   print   first   include   count   题意   using   

题意:

http://bjutacm.openjudge.cn/lianxi/s011/

思路:

使用stl中的map实现。map中保存每个会员的实力值和id组成的pair,对于每个新会员二分查找与之实力值最相近的老会员。

实力值相同的会员只需保存id最小的那个即可。

实现:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <map>
 4 #include <cmath>
 5 using namespace std;
 6 
 7 int n, id, d;
 8 map<int, int> m;
 9 
10 int main()
11 {
12     m.insert(pair<int, int>(1000000000, 1));
13     scanf("%d", &n);
14     for (int i = 0; i < n; i++)
15     {
16         scanf("%d %d", &id, &d);
17         map<int, int>::iterator it = m.lower_bound(d);
18         printf("%d ", id);
19         if (it == m.end())
20         {
21             it--;
22             printf("%d\n", it->second);
23         }
24         else if (it == m.begin())
25         {
26             printf("%d\n", it->second);
27         }
28         else
29         {
30             int tmp = abs(it->first - d);
31             int res = it->second;
32             it--;
33             int tmp2 = abs(it->first - d);
34             if (tmp2 < tmp || (tmp2 == tmp && it->second < res))
35             {
36                 res = it->second;
37             }
38             printf("%d\n", res);
39         }
40         if (!m.count(d) || m[d] > id)
41         {
42             m[d] = id;
43         }
44     }
45     return 0;
46 }

 

冷血格斗场

标签:bsp   strong   pair   print   first   include   count   题意   using   

原文地址:http://www.cnblogs.com/wangyiming/p/6384752.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!