TLE from unordered_set C++

Правка en1, от haochenkang, 2022-03-24 21:25:01

Hello Codeforces community!

I have a question regarding question B in the recent CodeTon round.

My solution passed all pretests, but TLE on test 22 in the main tests. My code is below. ~~~~~ void run_case(){ int n, k; cin >> n >> k; vector v(n); for(auto &it : v) cin >> it;

unordered_set<ll> seen;
for(int i = 0; i < n; i++){
    if(seen.count(v[i] + k) || seen.count(v[i] - k)){
        cout << "YES\n";
        return;
    }
    seen.insert(v[i]);
}  
cout << "NO\n";

} ~~~~~

I used an unordered_set to keep tracking of the seen elements. However, when I changed it to set, the solution passed main tests too. I have always thought that unordered_set are faster than set. Can someone please explain to me what's happening here?

Thanks for any help!

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en6 Английский haochenkang 2022-03-24 21:39:30 5 Tiny change: 'ered_set` are faster th' -> 'ered_set` is faster th'
en5 Английский haochenkang 2022-03-24 21:28:06 0 (published)
en4 Английский haochenkang 2022-03-24 21:27:46 0
en3 Английский haochenkang 2022-03-24 21:27:45 373 (saved to drafts)
en2 Английский haochenkang 2022-03-24 21:26:03 10 Tiny change: 'n \n}\n~~~~~\n\nI used' -> 'n \n}\n\n\nI used'
en1 Английский haochenkang 2022-03-24 21:25:01 935 Initial revision (published)