E. Jumping

Codeforces
IDCF10114E
Time10000ms
Memory512MB
Difficulty
English · Original
Formal · Original
Shopping with the wife is one of men's hardest marriage responsibilities. Nour is a good husband. So he goes out with his wife every Friday to "Damasquino" mall. Damasquino is a very famous mall in Damascus. It has a very unique transportation method between shops. Since the shops in the mall are laying in a straight line, you can jump on a very advanced trampoline from the shop i, and land in shop (i + di) or (i - di) depending on the direction of the jump. Where di is a constant given for each shop. You can jump forward or backward only, and you can't land any where before the first shop or after the last shop in the mall. There are N shops in the mall, numbered from 1 to N. Nour's wife starts her shopping journey from shop 1 and ends it in shop N. Unfortunately, Nour sometimes gets lost from his wife (the mall is very crowded on Fridays). So he wants to know for each shop, what is the minimum number of trampoline jumps he has to make in order to reach shop N and see his wife again! The first line consists of one integer T, the number of test cases. Each test case consists of two lines, the first line contains a single integer (2 ≤ N ≤ 105) the number of shops in the mall. The second line contains N integers. Where the ith integer (1 ≤ di ≤ N) is the constant described above for the ith shop. For each test case, print N lines. The ith line should contain one integer, the minimum number of jumps needed to be made in order to reach shop N starting from shop i, or  - 1 if it is impossible. Large I/O files. Please consider using fast input/output methods. ## Input The first line consists of one integer T, the number of test cases. Each test case consists of two lines, the first line contains a single integer (2 ≤ N ≤ 105) the number of shops in the mall. The second line contains N integers. Where the ith integer (1 ≤ di ≤ N) is the constant described above for the ith shop. ## Output For each test case, print N lines. The ith line should contain one integer, the minimum number of jumps needed to be made in order to reach shop N starting from shop i, or  - 1 if it is impossible. [samples] ## Note Large I/O files. Please consider using fast input/output methods.
**Definitions** Let $ T \in \mathbb{Z} $ be the number of test cases. For each test case: - Let $ N \in \mathbb{Z} $, $ 2 \leq N \leq 10^5 $, denote the number of shops. - Let $ d = (d_1, d_2, \dots, d_N) $, where $ d_i \in \mathbb{Z} $ and $ 1 \leq d_i \leq N $, be the jump constant for shop $ i $. **Constraints** 1. $ 1 \leq T \leq \text{large} $ (implied by large I/O). 2. For each shop $ i \in \{1, \dots, N\} $: - From shop $ i $, a jump of length $ d_i $ is allowed to shop $ i + d_i $ (forward) or $ i - d_i $ (backward). - Jumps must land within bounds: $ 1 \leq \text{target} \leq N $. **Objective** For each shop $ i \in \{1, \dots, N\} $, compute the minimum number of jumps required to reach shop $ N $, starting from shop $ i $. If unreachable, output $ -1 $. Formally, define $ f(i) $ as the minimum number of jumps from shop $ i $ to shop $ N $. Then: $$ f(i) = \begin{cases} 0 & \text{if } i = N \\ \min \left( \left\{ f(i + d_i) + 1 \mid i + d_i \leq N \right\} \cup \left\{ f(i - d_i) + 1 \mid i - d_i \geq 1 \right\} \right) & \text{if at least one valid jump exists} \\ \infty & \text{otherwise} \end{cases} $$ Output $ f(i) $ for $ i = 1 $ to $ N $, with $ \infty \rightarrow -1 $.
API Response (JSON)
{
  "problem": {
    "name": "E. Jumping",
    "description": {
      "content": "Shopping with the wife is one of men's hardest marriage responsibilities. Nour is a good husband. So he goes out with his wife every Friday to \"Damasquino\" mall.  Damasquino is a very famous mall in ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 10000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10114E"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Shopping with the wife is one of men's hardest marriage responsibilities. Nour is a good husband. So he goes out with his wife every Friday to \"Damasquino\" mall. \n\nDamasquino is a very famous mall in ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ T \\in \\mathbb{Z} $ be the number of test cases.  \nFor each test case:  \n- Let $ N \\in \\mathbb{Z} $, $ 2 \\leq N \\leq 10^5 $, denote the number of shops.  \n- Let $ d = (d_1, d_2,...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments