题意:一个定长范围内只能有一个数。这里可以用set对一个区间进行合并。
算法:
1.重载运算符,修改排序操作.
2.在两个点之间的距离小于等于k的时候直接返回false,表示不用装进去了,就不要这个数了。
3.否则,按照升序排列.
struct cmp
{
bool operator () (const int& u, const int& v) const
{
//若任意两个指纹相差小于k,则不用装进去了
if (abs(u - v) <= k)
return false;
//否则,还是按照升序排列
return u < v;
}
};
然后模拟就完了.
#include<bits/stdc++.h>
using namespace std;
int m, k;
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
struct cmp
{
bool operator () (const int& u, const int& v) const
{
//若任意两个指纹相差小于k,则不用装进去了
if (abs(u - v) <= k)
return false;
//否则,还是按照升序排列
return u < v;
}
};
//自定义set的排序
set<int, cmp> s;
int main(int argc, char const *argv[])
{
IOS;
cin >> m >> k;
while(m--)
{
string str;
int x;
cin >> str;
cin >> x;
if (str == "add")
{
s.insert(x);
}
else if (str == "del")
{
s.erase(x);
}
else
{
if (s.find(x) != s.end())
{
cout << "Yes" << endl;
}
else
cout << "No" << endl;
}
}
return 0;
}