牛客小白月賽6 G 指紋鎖 set的自動排序 模板
阿新 • • 發佈:2018-08-23
加密 的人 算法 esc code 超過 fin 代碼 sync
現在有3種操作,共m個,
操作1:add x,表示為指紋鎖錄入一個指紋,該指紋對應的數字為x,如果系統內有一個與x相差≤k的指紋,則系統會忽略這次添加操作
操作2:del x,表示刪除指紋鎖中的指紋x,若指紋鎖中多個與x相差≤k的指紋,則全部刪除,若指紋鎖中沒有指紋x,則可以忽略該操作,
操作3:query x,表示有一個持有指紋x的人試圖打開指紋鎖,你需要設計一個判斷程序,返回該人是否可以打開指紋鎖(只要x與存入的任何一個指紋相差≤k即可打開鎖)。
初始狀態,指紋鎖中沒有任何指紋。
鏈接:https://www.nowcoder.com/acm/contest/136/G
來源:牛客網
題目描述
HA實驗有一套非常嚴密的安全保障體系,在HA實驗基地的大門,有一個指紋鎖。 該指紋鎖的加密算法會把一個指紋轉化為一個不超過1e7的數字,兩個指紋數值之差越小,就說明兩個指紋越相似,當兩個指紋的數值差≤k時,這兩個指紋的持有者會被系統判定為同一個人。現在有3種操作,共m個,
操作1:add x,表示為指紋鎖錄入一個指紋,該指紋對應的數字為x,如果系統內有一個與x相差≤k的指紋,則系統會忽略這次添加操作
操作2:del x,表示刪除指紋鎖中的指紋x,若指紋鎖中多個與x相差≤k的指紋,則全部刪除,若指紋鎖中沒有指紋x,則可以忽略該操作,
操作3:query x,表示有一個持有指紋x的人試圖打開指紋鎖,你需要設計一個判斷程序,返回該人是否可以打開指紋鎖(只要x與存入的任何一個指紋相差≤k即可打開鎖)。
初始狀態,指紋鎖中沒有任何指紋。
輸入描述:
第一行有2個正整數m,k。
接下來m行,每行描述一種操作:add x,del x或query x。
輸出描述:
對於每個query操作,輸出一行,包含一個單詞“Yes”或“No”,表示該人是否可以打開指紋鎖。示例1
輸入
復制4 3 add 1 add 10 query 5 query 4
輸出
復制No Yes示例2
輸入
復制4 3 add 1 query 4 del 1 query 4
輸出
復制Yes No示例3
輸入
復制6 3 add 10 query 10 add 5 query 5 del 7 //系統將指紋10和指紋5全部刪除 query 8
輸出
復制Yes Yes No
備註:
對於100%的測試數據:
1 ≤ k,m ≤ 1000000
數據量較大,註意使用更快的輸入輸出方式。
分析:賽後看別人的代碼,get到了新知識點,set可以自動定義排序還可以定義是否可以插入。。
AC代碼:
#include <map> #include <set> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <vector> #include <string> #include <bitset> #include <cstring> #include <iomanip> #include <iostream> #include <algorithm> #define ls (r<<1) #define rs (r<<1|1) #define debug(a) cout << #a << " " << a << endl using namespace std; typedef long long ll; const ll maxn = 1e6+10; const double eps = 1e-8; const ll mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const double pi = acos(-1.0); ll m, k; struct cmp { //自定義排序規則,差值小於k的加不進集合 bool operator() ( ll a, ll b ) { if( abs(a-b) <= k ) { return false; } return a < b; } }; int main() { ios::sync_with_stdio(0); cin >> m >> k; set<ll,cmp> s; while( m -- ) { string str; ll x; cin >> str >> x; if( str[0] == ‘a‘ ) { if( s.find(x) == s.end() ) { s.insert(x); } } else if( str[0] == ‘d‘ ) { s.erase(x); } else { if( s.find(x) != s.end() ) { cout << "Yes" << endl; } else { cout << "No" << endl; } } } return 0; }
牛客小白月賽6 G 指紋鎖 set的自動排序 模板