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

Автор ShaFeiii, история, 34 часа назад, По-английски

When I tried to upsolve this problem 706B - Interesting drink I faced an undefined time limit!!

In my first approach, I solved it using a multiset and upperbound 294508423 I got time limit on test 11

then, tried to solve with vector and sorting, which got accepted 294508512

Can anyone help me understand what the issue was?

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

»
34 часа назад, # |
Rev. 2   Проголосовать: нравится +8 Проголосовать: не нравится

auto price = prices.upper_bound(x); int canBuy = distance(prices.begin(), price);

The calculation of canBuy takes $$$O(n)$$$ time in the worst case instead of $$$O(\log n)$$$ as you suppose, where n = prices.size(), since the time complexity of distance(prices.begin(), price) is $$$O(n)$$$ in the worst case, because these iterators are not random access.

Time complexity of std::distance( InputIt first, InputIt last ) is constant only if InputIt is the random access iterator, else it is linear.

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

    thx bro <3