一是因为unordered_map本身自带大常数,有时候跑的甚至没有map快。 第二个原因也是最重要的原因是,很多 CF 上的大神直接根据 STL 的源代码来造出 hack 数据,导致单次复杂度劣化成O(n)O(n)。 如果你很勇直接用 unordered_map 那么你就等着 fst 吧。 那有没有防止被卡的方法呢? 当然有,那就是自己写哈希...
undered_map 使用拉链法 hash,可以通过改变初始大小和负载因子来变快。可以变快约 1/5。 unordered_map<int, int> mp; mp.reserve(1024); mp.max_load_factor(0.25); 防hack 参考文章:Blowing up unordered_map, and how to stop getting hacked on it在cf 等比赛中可以 hack 别人的 unordered_map ...
一查测试数据,才发现竟然是阴间hack数据 在这种构造的数据冲突下直接退化成线性速度了,这我们肯定受不了,事实上我们只要添加上两行代码 unordered_map<int, int> mp; mp.reserve(1024); mp.max_load_factor(0.25); 就可以避免针对默认参数的hack,同时unordered_map的速度可以提升约10倍。 当然平时还是别偷懒加...
In these cases, it's pretty hard to make a proper hack case since we can't really insert a lot of distinct elements with the same modded key value. But depending on the problem and the code, it can still be slowed down to some extent. Suppose the number of elements is nn and the...
Hack It! 我们用前面说过的那个卡掉代码的质数来做输入文件。输入的数字为126271的1~200000倍。然后分别使用map和unordered_map 来跑。看看结果怎样?(在其他oj创建的题目,速度应该也差不多) 测试数据下载:input_file 没错,你没有看错,足足跑了4s!
This caused me think about which situations I should use an unordered_map instead of a regular map (ordered map). Is an unordered_map always inherently faster than a map? in which cases should I use it? I'm asking here because I couldn't get a satisfactory answer after searching the we...
记得在codeforces上交代码的时候大佬们会对使用unordered_set/unordered_map 的代码进行Hack,过程参考如下文章: Blowing up unordered_map, and how to stop getting hacked on it - Codeforcescodeforces.com/blog/entry/62393 其实也是利用了位置是哈希对桶数取模这一点。GCC的unordered_set/unordered_map 桶数...
可以,但不建议。unordered_map容易被卡,参考codeforces比赛。要么重新散列函数,要么直接用map、set吧据...
可以,但不建议。unordered_map容易被卡,参考codeforces比赛。要么重新散列函数,要么直接用map、set吧 ...