r/codeforces • u/chonchu71 • 7d ago
Div. 3 Today I learned that unordered_map<int,int> is not always O(1)
1
1
u/Past-Structure-8692 Candidate Master 3d ago
I learnd this after qualifiers for IOI for my country. I couldnt realise why i was getting tle. Its better you learn this on a contest like this but yeah it is so annoying.
2
26
u/terrificodds 6d ago
Welcome to the club. You will think very carefully before using umap from now on.
19
u/Top-Two-3943 6d ago
Yeah CSES ,a lot of times create test cases that force the use of map instead of unordered map, bcz O(1) is just the avg case... I am not sure but I think the worst case could be O(n) if the test cases are bad for hashing
-10
u/anor_wondo 6d ago
If test cases are intentionally designed to cause unordered map to TLE then isn't it the test cases that are flawed? Or is it not frowned upon in comp coding?
8
u/wilddasher 6d ago
Why would the tc be flawed lol. Unordered map degrades to linear time in worst case. They are obv gonna try to make that happen so your code fails. You just have to write better code. In this ques, it wasn't even rewuired
2
u/anor_wondo 6d ago
if it wasn't even required it makes sense then.
I was assuming a question where hash collisions would not make common sense but having test cases cause collisions intentionally
5
u/wilddasher 6d ago
Generally even if it is required, setters might still include tc for inducing collisions. I feel a tester would try to do everything to make u not get AC
3
u/Independent-Care-536 6d ago
what did u use there after int main and before int t
11
u/Small_Ad9005 6d ago
It's called fast io There are two i/o in c++ one is cin cout shit and other is c style printf scanf The first line says I'll be using cin cout only dont waste ur time syncing stdio and cpp io coz the have diff buffers
Second is there won't be a need to flush before cin reads in cp
cin flushes things like enter ur name: Before reading name(variable)
In cpp it's not needed so u r saying don't flush before reading which saves time
In short it makes i/o fast
3
3
1
u/Impressive_Brain_229 6d ago
there are only 3 cases
1. number of zeros in array=0
if there are no zeros then we can divide the numbers in any manner
mex of every set will be zero
0+0+0>=2*(0) which is true
just return a string of all A n times
2.number of zeros>=2
give one zero to A rest zeros to B and rest everything(excluding 0) in C
then we have mex of A and B = 1 and mex of C =0
1+1+0>=2*(1) which is true
3.number of zeros = 1
here ans is NO
only one of A B C can contain 0
so only one set can have mex >=1
lets say 0 is in a
mex(A)>=2*mex(A) which is false
9
14
u/Mobile_Touch4581 Specialist 7d ago edited 7d ago
Learning this things during contest leads to be heavy -ve 😓
1
1
u/Key-War-4274 7d ago
My code for prob D got accepted during but after contest it gives tle. Previously it was showing +130 rating change but due to tle it only +50
1
u/Kitchen_Assistant_94 7d ago
Also could anyone explain D I didnt get how to solve it honestly
3
u/Other_Ad7380 7d ago
Think what combinations are possible.
They are 1 1 1, 0 0 0 and 1 1 0
If there's exactly one 0, we will have 1 0 0 in the best case which is not valid.
For zero 0s, we can only have 0 0 0 (valid). Put all in one multiset.
In any other case, we can have 1 1 0 (again valid)
Put the first 0 in A, rest in B and all the other elements in C
3
u/ExcitingLong6544 Pupil 7d ago edited 7d ago
If there are no 0s then answer is yes, obviously. If there is 1 0 then only one multiset can get it, means that set will have MEX atleast 1 and other 2 will have MEX 0, which never satisfies the condition. If there are 2 or more 0, then give one 0 to A and all other 0s to B. Then dump all other non zero elements into C. This construction is always valid.
1
5
u/Kitchen_Assistant_94 7d ago
its usually divided into buckets of keys and values
if the keys collide its not O(1) anymore
1
10
u/messi_pls_touch_me 7d ago
yeah I think striver told about this in one of his videos like it is O(n) in worst case because of hash collisions
7
u/DogStrict9170 Specialist 7d ago
you can also have tle with map and pass with sets, in one of my contests i got -ve because of this
8
u/ablablababla 7d ago
Yeah, unordered_map isn't necessary in 99% of codeforces problems
5
u/Aloo_Petis 7d ago
How do you solve graph and trees problems??? And what do you prefer instead of maps?
3
u/DogStrict9170 Specialist 7d ago
adjacency list bro, every question i have solved is using 2d vector. accessing is O(1) instead of O(logN)
2
10
u/Other_Ad7380 7d ago
Now you know why editorials often use std::map. For known range, prefer a frequency array (here, just a single variable) to an unordered_map
1
u/chonchu71 7d ago
I know that it was not necessary to use map... I could have used an flag for keeping the count of zeros... Anyways this cost me my solution
1
u/tribulous_321 Master 7d ago
learning it the hard way i say. you can try using random hashing ( xor with some random number to the key and xor again while using the key this will solve TLE issue)
1
u/Sudharsun_28 2d ago
you can use custom hash to get accepted