zoukankan      html  css  js  c++  java
  • 冷血格斗场

    题意:

    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
    ", it->second);
    23         }
    24         else if (it == m.begin())
    25         {
    26             printf("%d
    ", 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
    ", res);
    39         }
    40         if (!m.count(d) || m[d] > id)
    41         {
    42             m[d] = id;
    43         }
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    css3记事
    ele
    vue记事1
    HBuilder
    继承与面向对象设计
    实现
    设计与声明
    资源管理
    构造/析构/赋值运算
    让自己习惯C++
  • 原文地址:https://www.cnblogs.com/wangyiming/p/6384752.html
Copyright © 2011-2022 走看看