题目:EPI
参考【哈希表】用O(1)时间执行哈希表的setAll操作(即把所有值置为一个统一值)
同样是需要设置一个全局偏移量,才能在O(1)时间完成setAll操作
class client
{
public:
int offset;//全局偏移量
unordered_map<string, int> M;//尽量用unordered容器
map<int, unordered_set<string>> M_credit;//为了max()在O(1)时间完成,需要自带排序的map容器
client() :offset(0){}
void insert(const string &s, const int &c)
{
M[s] = c - offset;
M_credit[c - offset].emplace(s);
}
bool remove(const string &s)
{
if (M.find(s) == M.end())
return false;
M_credit[M[s]].erase(s);
M.erase(s);
return true;
}
int loohup(const string &s)const
{
auto it = M.find(s);
if (it == M.end())
return -1;
else
return it->second+offset;
}
void addAll(const int a)
{
offset += a;
}
string max()const
{
//不要忘了判断容器M_credit是否为空
if (M_credit.empty() || M_credit.crbegin()->second.empty())
return "";
auto s = M_credit.crbegin()->second.begin();
return *s;
}
};