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

Автор farmersrice, история, 8 лет назад, По-английски

So, I was recently trying to solve this problem: http://mirror.codeforces.com/contest/727/problem/F submission at http://mirror.codeforces.com/contest/727/submission/21647053

My algorithm should be O(n2 * log1012 + mlogn). First, I determine the lowest possible prefix sum for a certain amount of removed problems. This is done with a binary search and greedy. I binary search on the condition "If we can remove a certain number of problems, can the minimum prefix sum be greater than or equal to the target?" (This is always of form TTTT...FFF). To find that condition I greedily add numbers to a running sum, and if the running sum is less than or equal to target, I remove the smallest value that occurs before or at the current index. Then, I read all m queries and binary search for the correct answer on those.

Can anyone help me understand why I'm getting TLE? Thanks.

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

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

Auto comment: topic has been updated by farmersrice (previous revision, new revision, compare).

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

First, your complexity analysis is slightly incorrect: it's , because you have a priority queue inside your binary search.

And that's precisely where your TLE comes from. Java standard library collections are usually quite slow, so you would have had better luck using your own heap implementation: 21686518

(If you don't want to rewrite the whole standard library, perhaps you might want to switch to C++? :P)

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

    No way. It's never been a problem of Java or C++. You can calculate the minimum initial mood required so that it will never drop to negative when reading from problem i to the end using dp.

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

    Ah, I understand now. Thanks for the help! (By the way, if there's any IDE that has auto-import in C++, could you please tell me?)

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

      Well, what basically all C++ coders do is just include the whole standard library with the line #include <bits/stdc++.h>. The only possible downside to that would be a higher probability of some standard function having name clashes with the variables in your program, but if you're really worried about that you can leave them all in the std namespace.

      To actually answer the question, I think CLion has something of the sort, but that IDE seems to be free only for students.

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

    One could simulate a decently-performing priority queue in Java using TreeSet. A way to comply with the definition of totally ordered set (a set is not exactly a priority queue) is to implement some class with values <key, index> and an appropriate comparison method.