HDU/4585/ Shaolin STL map application

Article Directory


The Shaolin
Insert picture description here
topic is about
monks fighting. The first old monk is numbered 1 and the combat power is 1e9. The following monks fight with them. The combat value should be similar to the combat value of the newcomer. Then the newcomer becomes the old monk. If the two old monks have the same combat value , Output the smaller value, and finally output the number of the newcomer and the number of the monk fighting with it.

Direct map mapm a p .
Set the monk’s combat power directly as thekey keykey i d id i d is the saveddata datad a t a , divided into two,map mapWhen there are multiple elements in the m a p container, take thekey keyk e y smalldata datadata

#include <bits/stdc++.h>
#define s second
#define f first
using namespace std;
const int maxn=1e9;
int n;
map<int,int>mp;
int main()
{
    
    
    while(~scanf("%d",&n) && n)
    {
    
    
        mp.clear();
        mp[maxn] = 1;
        int id,k;
        while(n--)
        {
    
    
            int ans;
            scanf(" %d%d",&id,&k);
            printf("%d ",id);
            map<int,int>::iterator it1,it2;
            it1 = mp.lower_bound(k);   //返回一个迭代器,指向键值大于k的第一个元素
            if(it1 == mp.begin()) ans= it1->s;
            else
            {
    
    
                it2=it1;
                if(it2->f - k >= k-(--it1)->f ) ans = it1->s;
                else ans = it2->s;
            }
            printf("%d\n",ans);
            mp[k] = id;
        }
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Edviv/article/details/108012048