NC17508 指纹锁

题意:一个定长范围内只能有一个数。这里可以用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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值