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

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

I have been trying this dp problem . But couldn't figure out the recurrence relation. The problem description is given below:

Problem Statement:

A long, linear field has N (1 <= N <= 1,000) clumps of grass at unique integer locations on what will be treated as a number line.Think of the clumps as points on the number line.

Bessie starts at some specified integer location L on the number line (1 <= L <= 1,000,000) and traverses the number line in the two possible directions (sometimes reversing her direction) in order to reach and eat all the clumps. She moves at a constant speed (one unit of distance in one unit of time), and eats a clump instantly when she encounters it.

Clumps that aren't eaten for a while get stale. We say the "staleness" of a clump is the amount of time that elapses from when Bessie starts moving until she eats a clump. Bessie wants to minimize the total staleness of all the clumps she eats.

Find the minimum total staleness that Bessie can achieve while eating all the clumps.

Input:

  • Line 1 : Two space-separated integers: N and L.
  • Lines 2..N+1: Each line contains a single integer giving the position P of a clump (1 <= P <= 1,000,000).

Output:

  • Line 1: A single integer: the minimum total staleness Bessie can achieve while eating all the clumps.

Sample Input:

4 10

1

9

11

19

Sample output:

44

Hint:

INPUT DETAILS: Four clumps: at 1, 9, 11, and 19. Bessie starts at location 10.

OUTPUT DETAILS: Bessie can follow this route:

  • start at position 10 at time 0
  • move to position 9, arriving at time 1
  • move to position 11, arriving at time 3
  • move to position 19, arriving at time 11
  • move to position 1, arriving at time 29
Теги dp, poj
  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

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

First of all, it should be clear that Bessie should eat a clump of grass as soon as she reaches it. Now assume Bessie starts between clumps i and i + 1. Then we can create a dp state dp[l][r][k] where l is the index of the leftmost clump of grass she hasn't eaten, and r is similarly defined. The k represents Bessie's current location (0 if on the leftmost clump of grass, 1 otherwise). Transitions are clearly O(1) so this solution works.

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

    The main idea is correct, though there's still a very crucial (and hard to spot) detail missing here.

    The answer is the sum of all times you eat the clumps, so you can think of it this way: Every minute that passes, values of all uneaten clumps increase by one, and the answer is the final value of all the clumps. So actually, every minute that passes contributes u to the final answer, where u is the amount of uneaten clumps. If we are at state l, r, k, then u = N - (r - l + 1).

    The transitions should be clear.

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

      can you please write out the transition equation?

      Edit: I have understood the solution. Thank you very much :)