洛谷P5250 【深基17.例5】木材仓库

    科技2023-10-15  97

    题目链接:https://www.luogu.com.cn/problem/P5250


    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> using namespace std; int n; map<int, int> h; int main(){ cin >> n; while (n -- ){ int a, b; cin >> a >> b; if (a == 1){ if (h.count(b)) puts("Already Exist"); else h[b] = 1; } else { if (h.empty()) puts("Empty"); else if (h.count(b)){ h.erase(b); cout << b << endl; } else { h[b] = 1; auto it = h.find(b); auto it2 = it; it ++; if (it2 == h.begin()){ cout << it->first << endl; h.erase(it); } else if (it == h.end()){ cout << (-- it2)->first << endl; h.erase(it2); } else if (b - (--it2)->first > it->first - b){ cout << it->first << endl; h.erase(it); } else { cout << it2->first << endl; h.erase(it2); } h.erase(b); } } } return 0; }
    Processed: 0.018, SQL: 8