Блог пользователя Raising_Heaven

Автор Raising_Heaven, история, 12 месяцев назад, По-английски

So I was stuck in this problem 2053B - Outstanding Impressionist for a long time; finally I am able to solve this problem a little while ago. I have used hashing technique to solve it. Firstly, let me explain how I have solved it. (If you do not want to hear me chattering about my solution approach, then skip the next two paragraphs.)

I have noticed that whenever li and ri are equal, it is guaranteed that there is a wi with the value of li (or ri) exists in the impression. If the same equal value of li and ri appear again, that means there must be multiple instances of the same value of wi is present in the impression, which means the ith piece of the impression must not be unique; otherwise, it will be unique as all the other pieces can be set to different values other than that of wi. An easy way to implement this logic would be to use hashing and store the frequency of those values of wi such that li = ri. If the frequency of wi is greater than 1, then wi is not unique; and if it is 1, then it is unique.

Now what happens when li and ri are not equal? In that case, we have to check if there exists any v such that li ≤ v ≤ ri and for any 1 ≤ j ≤ n if lj = rj, then v ≠ lj. It is because if such v exists, then we can set all the values of the other pieces of impression not equal to v and set wi to equal to v and hence, wi will be unique. Now how to implement this logic? At first, I have stored every possible values of wi in increasing order in a vector which are not guaranteed to be present in the impression. (Only when li = ri, a piece of impression of the value wi = li is guaranteed to be present in the impression.) I can do this by iterating from 1 to 2n and check whether the value is already present (or have frequency greater than 0) in the hash array/map which was previously created to store the frequency of wi which was guaranteed to be present. It is possible because of the constraints 1 ≤ li ≤ ri ≤ 2n and li ≤ wi ≤ ri and n ≤ 2.105, which means to do this, I have to do at most 4.105 operations since the largest value of ri or wi is 4.105. After that, I have used binary search to look for the smallest value equal to or greater than li in the newly created vector (let this value be c) and checked if c is equal to or less than ri or not. If c is equal to or less than ri, then we can set the value of wi to be c and the other pieces of impression to be not equal to c and hence, wi will be unique. Otherwise, wi will not be unique.

So this was all the logic and idea I came up with to solve the problem. At first, I implemented the hashing using unordered map and the verdict I received was TLE on testcase 12. Here is the submission 322353823. After receiving TLE, I thought since my code has time complexity of O(nlogn) for using binary search n times, I have to somehow get rid of this binary search and do the searching in constant time. But I did not know how to do so and ended up with the notion that my approach was not right. Then for some reason, I decided to implement the hashing using vector. I did it because I thought although unordered map takes constant time on average to insert and update its elements, a hash array created using vector also takes constant time to do the same and also not on average but always. After doing so, my solution got accepted 322354822. So today I have learned a new lesson that hash array created using vector is faster than unordered map. But my question is: Why is there a significant gap in performance between these two methods? Although unordered map is slower, shouldn't it also pass the testcases? Since the accepted solution takes 93 ms to pass the 12th testcase and the time limit is 1s, does that mean vector is more than 10 times faster compared to unordered map? And when the input is in 106 range, should I always implement hashing using vector? When would using unordered map be appropriate? These are some questions that have arisen in my head. Any sort of help would be appreciated.

Полный текст и комментарии »

  • Проголосовать: нравится
  • -6
  • Проголосовать: не нравится