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

Автор daihan, 9 лет назад, По-английски

I solved this problem : They are everywhere using two pointer ,

but now i am trying to solve this problem using binary search . I am getting TLE on test 11 . Here is my code :

https://pastebin.com/jb5N0EKb .

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

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

Your binary search is too expensive for it's purposes. For each iteration, you add element from i to mid in a set, which makes the complexity nlogn. and since you do the iteration log n times, it will be nlognlogn. and since you have a for loop from 1 to n for this, it will become n^2log^2n, which clearly is too high for n = 1e5.

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

    How can i evaluate : number of distinct character from index i to index mid without inserting into set ? I did precalculation like partial sum ,but it does not work for some cases .

    • »
      »
      »
      9 лет назад, скрыть # ^ |
      ← Rev. 3  
      Проголосовать: нравится 0 Проголосовать: не нравится

      well there are a few ways, I will just suggest 2:

      1) use 26 segment tree for each character and do a range sum query of that range and see if it is 1.

      2) store the index of the character into 26 sorted array, and do a binary search for each character to see if within the range.

      Do note that this will make your total complexity 26*nlog^2n, which may not be in the time limit.

      Edit: something is wrong with me, you can do partial sum for each character and that will be O(1). silly me. I don't know why you say it do not work.