zoukankan      html  css  js  c++  java
  • 洛谷 P5250 【深基17.例5】木材仓库

    题目描述

    博艾市有一个木材仓库,里面可以存储各种长度的木材,但是保证没有两个木材的长度是相同的。作为仓库负责人,你有时候会进货,有时候会出货,因此需要维护这个库存。有不超过 100000 条的操作:

    • 进货,格式1 Length:在仓库中放入一根长度为 Length(不超过 10^9109) 的木材。如果已经有相同长度的木材那么输出Already Exist
    • 出货,格式2 Length:从仓库中取出长度为 Length 的木材。如果没有刚好长度的木材,取出仓库中存在的和要求长度最接近的木材。如果有多根木材符合要求,取出比较短的一根。输出取出的木材长度。如果仓库是空的,输出Empty

    输入格式

    输出格式

    输入输出样例

    输入 #1
    7
    1 1
    1 5
    1 3
    2 3
    2 3
    2 3
    2 3
    输出 #1
    3
    1
    5
    Empty

    分析
    set练习题

    代码
    #include<bits/stdc++.h>
    using namespace std;
    
    int n,m,q;
    set<int> st;
    
    int main()
    {
        cin>>q;
        while(q--)
        {
            cin>>n>>m;
            if(n==1)
            {
                int f=st.size();
                st.insert(m);
                if(st.size()==f)
                {
                    cout<<"Already Exist"<<endl;
                }
                continue;
            }
            else if(n==2)
            {
                if(st.empty()) 
                {
                    cout<<"Empty"<<endl;
                    continue;
                }
                set<int>::iterator it,it2,it3;
                it=it2=it3=st.lower_bound(m);
                if(*it==m||it==st.begin())//存在相等
                {
                    cout<<*it<<endl;
                    st.erase(it);
                    continue;
                }
                else if(it==st.end())
                {
                    cout<<*(--it2)<<endl;
                    st.erase(*it2);
                    continue;
                }
                else
                {
                    if(abs(*it-m)<abs(m-(*--it3)))//大根离目标更近 
                    {
                        cout<<*it<<endl;
                        st.erase(*it);
                        continue;
                    }
                    else 
                    {
                        cout<<(*it3)<<endl;
                        st.erase(*it3);
                    }
                }
            }
        }
        return 0;
    }






  • 相关阅读:
    About LabView
    Accuracy, Precision, Resolution & Sensitivity
    EIT: where is it now and what lies ahead?
    <2014 12 28> Some conclusions and thought recently
    <2014 10 01> 数学基础 Wikipedia
    关于HashMap
    elasticsearch index 之 put mapping
    elasticsearch index 之 create index(二)
    elasticsearch index 之 create index(-)
    elasticsearch index 之merge
  • 原文地址:https://www.cnblogs.com/KyleDeng/p/15596550.html
Copyright © 2011-2022 走看看