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

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

Help pls!

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

»
6 месяцев назад, # |
Rev. 2   Проголосовать: нравится +32 Проголосовать: не нравится

You can keep an array of Boolean values, with each index representing whether or not you have seen this number in the array. It will be of size n+1, since mex of n elements is at most n+1. Fill this array in with a O(n) loop over the array, and then go over the Boolean array and find the first false value, that index will be your mex.

»
6 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

You can use cycle sort to put elements to it's corresponding index. Then traverse again and first element not equal to index is your mex.

Remember to ignore elements out of bound.

Time: O(N)
Space: O(1)

»
6 месяцев назад, # |
  Проголосовать: нравится +20 Проголосовать: не нравится

If there are $$$Q$$$ mex queries of ranges in an array lengthened $$$N$$$, then it can be solved in $$$O(Q\sqrt N+N)$$$ with Mo's algorithm.

»
6 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

in case of the given array is sorted, you can binary search on the first index that is not equal to it's value in the array, the answer will be that index. timeO(logN).

code
  • »
    »
    6 месяцев назад, # ^ |
      Проголосовать: нравится +10 Проголосовать: не нравится

    Besides being sorted, the array should only have unique values.

    • »
      »
      »
      6 месяцев назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      this can be easily handled while taking the initial array.

      code
»
6 месяцев назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

If you want to just find out the mex of the whole array, write a simple loop.

int mex(vector<int> vc) {
	vector<int> cnt;
	int n=vc.size(); cnt.resize(n+2);
	for(auto id:vc) if(vc<=n) cnt[id]=1;
	for(int i=0;i<=n;i++) if(cnt[i]==0) return i;
}

If you want to query the mex of an interval for many times, this problem may help. https://www.luogu.com.cn/problem/P4137

Sorry I don't know "可持久化线段树"'s English translation.

»
6 месяцев назад, # |
  Проголосовать: нравится -7 Проголосовать: не нравится

I think this video may be helpful. It is about different ways to find MEX of an array. From the simplest one to the most efficient

https://youtu.be/JDuVLyKn7Yw?si=CRqVObXm7JGdCMmg

»
6 месяцев назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

if array is permutation of lenght N, and one value is missing, you can just find total sum = n*(n+1)//2 and then subtract each element of array from sum. Final value of sum is going to be your MEX.

»
6 месяцев назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

Construct a hash array. Use Binary search and range query data structure such as minimum Sparse Table, check if in given range the minimum value is zero and accordingly update pointers.