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

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

Hi everyone!

I am trying to learn DP, and I stumbled across this question on TopCoder. While I have a hunch that it will involve bitmasks, the naive approach, i.e. generating all the sequences, simulating the matches and finding the solution would involve O(n.n!) time. And that's too much for 2 ≤ n ≤ 16.

I have some other unformed ideas, but I wanted to know how do I tackle such problems in general? Thanks!

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

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

Well, in my opinion there is no one unique way to tackle dynamic programming, or even more specially bitmask dynamic programming. In the particular example you have to find a way to count what you want without knowing the particular order of the players, but rather which players are available and which are taken. So bear in mind the following:

  • When you try to solve your subproblems you only care about subproblems of size 2^k
  • The limits are quite small so you can do pretty much anything you like of the order 2^k times something
  • Suppose you have calculated the number of ways for a person to win a tournament of size 2^k, how can you compose your results so as to calculate in how many ways a person can win a tournament of size 2 ^ ( k + 1 )?
  • »
    »
    11 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Thanks for your reply!

    So, basically I can have something like dp(p, k) which can give the number of wins for player p for the 2k case. So I have some insights for the 2k + 1 case -

    1. For any tournament of size 2k, the player p must have k wins over successive levels.
    2. Thus, there should be atleast k people who lose to p in the tournament. Then, (maybe) it's about finding the total number of orderings of those people. So, say if p wins over n people for n ≥ k, then a solution can be found. (which might be solved by calculating possible permutations).

    So, maybe I can try this : I find out over how many people now (in the 2k + 1 case) does p win. Then if the result is greater than or equal to k + 1, maybe permuations can computed.

    Please correct me (did you mean something like this?)

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

      Yes, something like that, but you miss information which do not allow to compose your subproblems of size 2^k to compute bigger subproblems. Where do you hold which people are in the current tournament and thus which people can p win?

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

        I guess I meant something like : for a tournament of size 2k, the people involved are 0, 1, 2, 3..2k - 1. So, for a new tournament of size 2k + 1, the new people are 2k, 2k + 1...2k + 1 - 1. So, if I can have an array of vectors arr, then arr[i] can hold the number of people that i can defeat. And for each subproblem, I can update arr according to the given matrix of who can defeat whom.

        For example, suppose I have k = 2, and 0 can defeat 2, 3. Then for k = 3, I need to check who in {4, 5, 6, 7} 0 can defeat, and update arr[0] accordingly.

        But still, I'm confused over how should I construct the solution for 2k + 1 given results for 2k.

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

          I didn't quite get the part with the array of vectors but concerning the first part, how can you be sure that the people involved in a tournament have contiguous indices? For all you know you can have a tournament with players 3, 9, 10 and 13 participating. ( Hint: you have to add one more dimension to your dp state )

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

            I thought so because it was given in the problem description that the people participating in a tournament of size N are numbered 0...N - 1.

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

              Yeah people participating in a tournament are numbered for 0 to N — 1 but after the first elimination round the indices that may occur may be random. Therefore you have to take that into account when you compute your dp state.

              I think that your state should be something like dp[ bit, k, i ] which means number of ways that player i wins a tournament of size 2^k with people participating given by bit which is a binary number. For example if bit = 1001011 people with indices 0, 1, 3 and 6 are participating. Then for every j such that i beats j we have dp[ bit1, k, i ] = Sum_j dp[ bit1, k — 1, i ] * dp[ bit2, k — 1, j ]. where bit2 AND bit1 = 0 ( there are no two people participating in the same tournament ).

              PS: now that i think of it you don't even need k as the size of the tournament is represented by the number of 1s in your bitmask.

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

There's a short editorial here: http://codeforces.me/blog/entry/19400

I coded a solution, and the general idea is that you split the number of people by 2 over and over again, and to merge the result by looking at the given win vector, and multiplying the ways for the one player to advance, and the other.