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

    热血

    1.问题分析

    每个人都有对应的实力值和id,而且每个人的实力值不同,用map<int,int>就可以了。键——实力值,值——id,排序后找实力最接近的或id更小的那个。

    2.解决方案

    ①map<int,int>用来存放实力值和对应的id②判断当前实力值是否为最小或最大③找到上(下)一个最接近的实力值,相等时选前一个。

    3.算法设计

    4.编程实现

    #include<iostream>
    #include<map>
    using namespace std;
    typedef map<int,int>M;
    map<int,int>::iterator it,pre,nex;
    int main()
    {
    int n;
    int id,strength;
    M player;
    player[1000000000]=1;
    cin>>n;
    while(n--)
    {
    cin>>id>>strength;
    player[strength]=id;
    it=player.find(strength);
    if(it==player.begin())
    {
    nex=it;
    nex++;
    cout<<it->second<<" "<<nex->second<<endl;
    }
    else if(it==player.end())
    {
    pre=it;
    pre--;
    cout<<it->second<<" "<<pre->second<<endl;
    }
    else
    {
    nex=it;
    nex++;
    pre=it;
    pre--;
    if(nex->first-it->first < it->first-pre->first)
    cout<<it->second<<" "<<nex->second<<endl;
    else
    cout<<it->second<<" "<<pre->second<<endl;
    }
    }
    return 0;
    }

    5.结果分析

    6.总结体会

    看完第四个题之后知道这个题中实力值没有重复的。还是考察对于map这种神奇的容器的应用,用起来还不是很顺手,时间长了应该就比较顺畅了。

    冷血

    1.问题分析

    每个人都有对应的实力值和id,根据题目要求来看,每个人的实力值可能是相同的,所以需要定义map<int,vector<int>>型的map用来存放实力值和对应的id。然后在根据题目要求中的实力最接近和id较小两个要求来写代码。

    2.解决方案

    ①map<int,vector<int>>用来存放实力值和对应的id②判断目前实力值是不是最小或最大③判断在目前的实力值中,是否有其余的id,如果有,选id[0]

    3.算法设计

    4.编程实现

    #include<iostream>
    #include<map>
    #include<vector>
    using namespace std;
    typedef map<int,vector<int>>M;
    map<int,vector<int>>::iterator it,pre,nex;
    int main()
    {
    int n;
    int id,strength;
    vector<int>tem1,tem2,tem3;
    M player;
    player[1000000000].push_back(1);
    cin>>n;
    while(n--)
    {
    cin>>id>>strength;
    player[strength].push_back(id);
    it=player.find(strength);
    tem1=it->second;
    if(it==player.begin())
    {
    if(tem1.size()==1)
    {
    nex=it;
    nex++;
    tem2=nex->second;
    cout<<tem1[0]<<" "<<tem2[0]<<endl;
    }
    else
    {
    int len=tem1.size()-1;
    cout<<tem1[len]<<" "<<tem1[0]<<endl;
    }
    }
    else if(it==player.end())
    {
     if(tem1.size()==1)
    {
     pre=it;
    pre--;
    tem2=pre->second;
    cout<<tem1[0]<<" "<<tem2[0]<<endl;
    }
    else
    {
    int len=tem1.size()-1;
    cout<<tem1[len]<<" "<<tem1[0]<<endl;
    }
    }
    else
    {
     if(tem1.size()==1)
    {
    pre=it;
    pre--;
    tem2=pre->second;
     nex=it;
    nex++;
    tem3=nex->second;
    if(nex->first-it->first > it->first-pre->first)
    cout<<tem1[0]<<" "<<tem2[0]<<endl;
    else
    cout<<tem1[0]<<" "<<tem3[0]<<endl;
    }
    else
    {
     int len=tem1.size()-1;
     cout<<tem1[len]<<" "<<tem1[0]<<endl;
    }
    }
    }
    return 0;
    }

    5.结果分析

    顺利(嘿嘿~)

    6.总结体会

    这个题主要考对于map的理解和掌握。map中的键是以红黑树排序的,前不久竟然还以为是一个数组,实在是太傻了。


      

  • 相关阅读:
    Excel生成二维折线图详细教程 TheChosen
    sql语句截取字段中某一符号前几位的方法? TheChosen
    python快速下载包的镜像源? TheChosen
    Android 超大图长图浏览库 SubsamplingScaleImageView 源码解析
    记录线上APP一个排序比较引发的崩溃 Comparison method violates its general contract!
    Android 内存泄漏检测工具 LeakCanary(Kotlin版)的实现原理
    改数组长度
    枚举数组所有组合
    Ubuntu20.04启动后光标一直闪烁
    harbor镜像仓库清理操作
  • 原文地址:https://www.cnblogs.com/19991201xiao/p/8908742.html
Copyright © 2011-2022 走看看