P5250 木材仓库(set)

题目传送门

题意: 给一些操作,对一些木材进行入库出库操作,出库的时候如果有这种,就直接出,否则出差距小的,若任有多种出库,出长度小的。

思路: 会一些set的基本用法就行。

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ls p<<1
#define rs p<<1|1
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
char *fs,*ft,buf[1<<20];
#define gc() (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<20,stdin),fs==ft))?0:*fs++;
inline int read(){
int x=0,f=1;char ch=gc();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=gc();}
return x*f;}
using namespace std;
const int N=1e5+5;
const int inf=0x7fffffff;
const int mod=998244353;
const double eps=1e-6;
signed main()
{
    set<int>s;
    int q;
    cin>>q;
    while(q--)
    {
        int op,len;
        cin>>op>>len;
        if(op==1)
        {
            if(s.count(len))
            {
                cout<<"Already Exist"<<endl;
            }
            else
            {
                s.insert(len);
            }
        }
        else
        {
            if(s.empty())
                cout<<"Empty"<<endl;
            else
            {
                auto p=s.lower_bound(len);
                auto pp=p;
                if(pp!=s.begin())
                    pp--;
                if(len-(*pp)>(*p)-len&&p!=s.end())
                    pp=p;
                cout<<*pp<<endl;
                s.erase(pp);
            }
        }
    }
}

原创文章 144 获赞 13 访问量 8666

猜你喜欢

转载自blog.csdn.net/Joker_He/article/details/106027416
今日推荐