Vijos p1471 instructor's playground (Greedy)

Source: Internet
Author: User
Transport: https://vijos.org/p/1471
Description

Members of orz instructors have created a playground for the instructors. Under the plan of the instructors, there are n lines of elastic and invincible jumping devices in one direction facing a huge lake, when people step on the device, they can take you to a place that is infinitely far away and enjoy the flight. However, when these devices are put into use, it is found that people who come to play prefer to jump on these devices, and because of the elastic advantages of these devices, not only can they make people jump far to the right direction, but also a certain distance to the opposite direction.

The instructor came up with a game where the N devices were numbered 1. N in the reverse order. Device I can jump to 1 .. i-1 device, and each device has a not necessarily the same direction of elastic A [I], representing the I device can also jump to the I + 1 .. I + A [I] device. The instructor specifies a starting device. If you need to step on the device several times in a row (the starting device is also included), you can jump to the back of the nth device, if the k-th device has K + A [I]> N, you can jump from the k-th device to the rear of the n-th device.

(PS: You can think that there are n + 1 devices, that is, how many times can be requested to n + 1 devices)

Format input format

The input row 1st contains two positive integers n, m, which are the number of devices and the number of inquiries.

The fifth line contains n positive integers. The I-TH positive integer is a [I], that is, the maximum jump length from the I-th device to the opposite direction.

The fifth row contains M positive integers. To ask which device to start with, it takes at least a few times to jump to the back of the nth number.

Numbers are separated by spaces.

Output Format

The output contains one row. This row has M positive integers. For each query, the number of devices that need to be step on is output, separated by spaces.

Line breaks at the end of a line without any extra spaces.

Example 1 input 1 [copy]
5 52 4 1 1 11 2 3 4 5
Sample output 1 [copy]
2 1 2 2 1
Restrictions

For 20% of the data, n ≤ 10;
For 40% of data, there are n ≤ 100, m ≤ 10;
For 60% of data, there are n ≤ 1000, A [I] ≤ 1000, m ≤ 500;
For 100% of data, there are n ≤ 100000, a [I] ≤ n, m ≤ 40000.


Ideas:

O (n) algorithm:
First, right-to-left find the leftmost point that can reach point N + 1 and set this point to I. Take this point as the left boundary and point N as the right boundary, there are only two vertices in this interval: 1. if you can directly jump to n + 1, the number of steps is 1; 2. otherwise, the number of steps is two (jump to I and then to n + 1); then, with the I-1 point as the right boundary, continue to the left to find the point that can reach the leftmost point of I, set as Point J, then the points in the interval (J, I-1) also have only two kinds of situations: 1. if you can jump to the I point, the number of steps is the number of I points + 1, 2. if not, the number of steps is I + 2 (first jump to J and then to I)
And so on, until the complexity O (n) is swept to the left boundary );

First, we want to find a point A with N on the leftmost side. If the point I on the right of a can jump out of N, then d [I]: = d [n + 1] + 1 else d [I]: = d [a] + 1; process once and then round n: = A-1, so Loop


Code:

/* Author £° ac _ Sorry problem: p1471 instructor's game */# include <cstdio> # include <iostream> # include <cstring> # include <cmath> # include <vector> # include <algorithm> # include <climits> # define INF int_maxusing namespace STD; int step [100010]; int A [100010]; int main () {int n, m; scanf ("% d", & N, & M ); for (INT I = 1; I <= N; I ++) {scanf ("% d", A + I) ;}int n = n + 1; step [N] = 0; while (n> 1) {int I; for (I = 1; I <n; I ++) {if (a [I] + I> = N) break;} Step [I] = step [N] + 1; for (Int J = I + 1; j <= N-1; j ++) {if (a [J] + j> = N) Step [J] = step [N] + 1; else step [J] = step [I] + 1;} n = I;} int Q; scanf ("% d", & Q); printf ("% d ", step [Q]); For (INT I = 1; I <m; I ++) {scanf ("% d", & Q); printf ("% d ", step [Q]);} printf ("\ n"); Return 0 ;}


Vijos p1471 instructor's playground (Greedy)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.