前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Shaolin(map)

Shaolin(map)

作者头像
dejavu1zz
发布2020-10-23 15:18:44
2290
发布2020-10-23 15:18:44
举报
文章被收录于专栏:奇妙的算法世界

题意描述

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 fighting skill is also taken into account. When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his. The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.

依次给出和尚序列,寻找与自己的战力相差最小的和尚,输出战斗的顺序

思路

由于之前只是简单学了map的使用,对于map的遍历没有研究过。这道题也是现学现做的。由于id和战力的唯一性,所以我们可以使用map来进行映射,由于map是有序的,所以map对应的映射可以为[战力:id],然后每次输入后利用lower_bound来寻找最接近的,然后比较即可。

AC代码

代码语言:javascript
复制
#include<bits/stdc++.h>
#define x first
#define y second
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=1e5+10;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
void solve(){
    int n;
    while(cin>>n && n){
        vector<PII> ans;
        map<int,int> mp;
        map<int,int>::iterator it1,it2,it3;
        mp[1000000000]=1;
        for(int i=0;i<n;i++){
            int idx,power;cin>>idx>>power;
            it1=mp.lower_bound(power);
            if(it1==mp.begin()){
                ans.push_back({idx,it1->second});
            }else{
                it2=it1;
                it3=--it1;
                if(abs(it2->first-power)>=abs(it3->first-power)){
                    ans.push_back({idx,it3->second});
                }else ans.push_back({idx,it2->second});
            }
            mp[power]=idx;
        }
        for(int i=0;i<ans.size();i++) cout<<ans[i].x<<' '<<ans[i].y<<endl;
    }
}
int main(){
    IOS;
    solve();
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/07/06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意描述
  • 思路
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档