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

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

You are given an array of n numbers

Find the longest consecutive subarray so that The Last and First member of the sub array is Neither the Smallest or the Biggest member of That subarray (not the entire array)

for example

1 3 2 4 6 5 7

the sub array [3, 2, 4, 6, 5]

in this subarray the smallest member is 2 and the biggest member is 6, the first member is 3 which is not equal to 2 or 6, the last member 5 is not equal to 2 or 6 either so this is a valid subarray, so you cout 2 6 (the l and r of the subarray)

The goal is to find a Valid subarray

the second Goal is to find the Biggest valid subarray

the numbers are from 0 up to 1e9 and n is from from 1 up to 2*1e5

My strategy is to make a two vector of vectors, saving the numbers sorted from index 0 up to i so you can always know the smallest and biggest member of each subarray and then just do a sliding window algorithm.

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

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

I believe this problem can be directly solved by removing either the minimum or maximum element from both ends of the array.

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

If someone has an easier approach, please share. I think we can do d&c. Suppose we split a segment in the middle, and the "optimal" segment lies over this split. Then it consists of a suffix of the left segment, and a prefix of the right segment. We have 2 cases:

  • One of the endpoints (left or right) is neither the smallest nor the largest element of the corresponding half.

  • The endpoints are the largest (but not smallest) and the smallest (but not largest) in their halfs.

For the first case, we iterate over the other side, and we only need to check the furthest prefix/suffix on the other side that satisfies the first case.

For the second case, we can do 2 pointers approach over the suffixes/prefixes that satisfy this case, and keep track of the furthest suffix/prefix that is still (maybe) possible. For example, if right endpoint is minimum in its half, and left endpoint is maximum, and H is the largest value in the right half, then from the suffix maxima on the left side, we point to the largest one that is strictly less than H. Then we need to check whether the minimum on the left side is less than the right endpoint.

This is O(n log(n)). I feel like there might be a way easier solution, so please tell me if so.

  • »
    »
    17 месяцев назад, скрыть # ^ |
     
    Проголосовать: нравится +8 Проголосовать: не нравится

    First choose all elements. If one endpoint is the smallest or the largest element, then we cannot let it is not by moving another endpoint. Then we can only remove this endpoint. Repeating this procedure is OK.

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

This problem is basically the same as 1793C - Даша и поиски

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

Nice problem btw