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

冷血格斗场

时间:2017-10-15 23:18:45      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:air   return   com   char   word   name   print   turn   space   

using namespace std;  
#include<iostream>  
#include<map>  
#include<vector>  
#include<fstream>  
int abs(int a)  
{  
    return a > 0 ? a : -a;  
}  
map<int, int> a;  
void findafter(vector<pair<int,int>>&b, map<int, int>::iterator p,int temp,int strength){  
    while ((p != a.end()) && (abs(p->first - strength) == temp))  
    {  
        b.push_back(*p); p++;  
    }}  
void findbefore(vector<pair<int, int>>&b, map<int, int>::iterator p1, int temp, int strength)  
{  
    while ((p1 != a.begin()) && (abs(strength - p1->first) == temp))  
    {  
        b.push_back(*p1); p1--;  
    }  
    if (p1 == a.begin() && (abs(strength - p1->first) == temp))  
        b.push_back(*p1);  
}  
  
void fight(int id, int strength) {  
    int id1 = 100000000;  
    map<int, int>::iterator p = a.lower_bound(strength);  
    if (p == a.begin())//所有老会员实力都大于新会员  
    {  
        vector<pair<int, int>> b; int temp = (p->first - strength);  
        findafter(b, p, temp, strength);  
        for (auto e : b)  
        {  
            if (e.second < id1)  
                id1 = e.second;  
        }  
    }  
    else {  
        map<int, int>::iterator p1 = --p;  
        p++;  
        if (p == a.end())  
        {  
            vector<pair<int, int>> c;  
            int temp =abs( strength - p1->first);  
            findbefore(c, p1, temp, strength);  
            for (auto e : c)  
            {  
                if (e.second < id1)  
                    id1 = e.second;  
            }  
  
        }  
        else {  
            int temp =abs (p->first - strength);  
            if (temp > abs(strength-p1->first)) {  
                id1 = p1->second;  
            }  
            else if (temp < abs(strength - p1->first))  
            {  
                id1 = p->second;  
            }  
            else {  
                  
                vector<pair<int,int>> d;  
                findbefore(d, p1, temp, strength);  
findafter(d, p, temp, strength);  
                    for (auto e : d)  
                {  
                    if (e.second < id1)  
                        id1 = e.second;  
                }}  
        }}  
    printf("%d %d\n", id, id1);  
}  
  
int main()  
{  
      
    int n;  
    cin >> n;  
    //scanf_s("%d", &n);  
    int id, strength;  
      
    a.insert(pair<int, int>(1000000000, 1));  
    for (int i = 1; i <= n; i++)  
    {  
        //scanf_s("%d %d", &id, &strength);  
        cin >> id >> strength;  
fight( id, strength);  
auto it = a.find(strength);  
if (it == a.end() || it->second>id)   a[strength] = id;  
  
    }  
} 

冷血格斗场

标签:air   return   com   char   word   name   print   turn   space   

原文地址:http://www.cnblogs.com/2201192792com/p/7674776.html

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