Leader of the playground

Leader of the playground

Time limit: 1 Sec   Memory Limit: 128 MB

Title Description

 Members Orz leader for a leader to build a playground, a leader in the planning, the playground has a row of n jump Elastic invincible, they are moving in a direction, in front of a huge lake, when people can step up device take you in that direction infinitely far away, and enjoy your flight. However, when these and other devices in use, they found that people prefer to play in jumping on these devices, since these devices and elasticity advantages, they can make people not only in the direction of the pair can jump very far, too can jump a certain distance in the opposite direction.
Then came up game leader, the order number to the direction opposite to the n 1..n means. The i-th device may skip one means 1..i-1, and each means has a direction opposite the elastic necessarily identical A [i], representative of the i-th apparatus may also skip the first i + 1..i + a [i] a device. Means a specified initial leader, Q From this device, the device requires a minimum of several successive step (Also includes starting means), can skip backward n-th device, i.e., if there are k means the k-th + a [i]> n, then the k-th device can skip to the rear of the n-th device.
(PS: You can think of n + 1 devices, i.e., how many times the first can section to the n + 1 devices request)
 

Entry

The first input line comprises two positive integers n, m, the number of devices and the number of inquiries.
Line 2 contains the positive integer n, the i is a positive integer A [i], i.e. the maximum jump length of the i-th unit in the opposite direction.
Line 3 contains m positive integer, from which means for the interrogation began, at least to several of the n-th backward skip.
Separated by spaces between numbers.
 

Export

Output contains a line, the line with m a positive integer, for each query, the output device requires a minimum number of the step, between the numbers separated by a space.
Wrap the end of the line and no extra spaces.
 

Sample input

5 5
2 4 1 1 1
1 2 3 4 5

Sample Output

2 1 2 2 1

prompt

When the first start means from a second skip means then can jump backward in the n-th device.
If the third means start from the same jump second device.
If the can jump start means or the second means from the last four devices, and then out of the n-th device 2 is the same answer.
[Data size]
for 20% of the data, there are n ≤ 10;
for 40% of the data, there n≤100, m≤10;
for 60% of the data, there n≤1000, a [i] ≤1000, m≤ 500;
to 100% of the data, there n≤100000, a [i] ≤n, m≤50000.

answer

Segment tree + DP, I was awkward to do so or is this problem? Each tree node is a kind of maximum values, which can be a bounce node updates the value of n is 1, n to a traverse start time (jump back) to find each point i to i + a [i] a minimum value, and then used to update the value of node i;

Then a traverse from 1 to n (jump ahead), to find the minimum value of i is 1, then used to update the value of node i, and each point is determined so that the minimum number of steps required of it out of n.

 1 #include<cstdio>
 2 #include<iostream>
 3 #define INF 0x7fffffff
 4 using namespace std;
 5 const int N = 100009;
 6 int t[4*N];
 7 int n, m, a[N];
 8 void build(int L, int R, int rt)
 9 {
10     t[rt] = INF;
11     if(L >= R) return;
12     int lc = rt << 1;
13     int rc = rt << 1 | 1;
14     int mid = (L + R) >> 1;
15     build(L, mid, lc);
16     build(mid+1, R, rc);
17     return;
18 }
19 void update(int L, int R, int rt, int to, int val)
20 {
21     if(R < to || L > to || L > R) return;
22     if(L == R)
23     {
24         t[rt] = min(t[rt], val);
25         return;
26     }
27     int lc = rt << 1;
28     int rc = rt << 1 | 1;
29     int mid = (L + R) >> 1;
30     if(mid >= to) update(L, mid, lc, to, val);
31     else update(mid+1, R, rc, to, val);
32     t[rt] = min(t[lc], t[rc]);
33     return;
34 }
35 int query(int L, int R, int rt, int l, int r)
36 {
37     if(R < l || L > r || L > R) return INF;
38     if(l <= L && R <= r) return t[rt];
39     int lc = rt << 1;
40     int rc = rt << 1 | 1;
41     int mid = (L + R) >> 1;
42     int lv = query(L, mid, lc, l, r);
43     int rv = query(mid+1, R, rc, l, r);
44     t[rt] = min(t[lc], t[rc]);
45     return min(lv, rv);
46 }
47 int main()
48 {
49     scanf("%d%d", &n, &m);
50     build(1, n, 1);
51     for(int i = 1; i <= n; i++)
52     {
53         scanf("%d", &a[i]);
54         if(i + a[i] > n)
55             update(1, n, 1, i, 1);
56     }
57     for(int i = n; i > 0; i--)
58     {
59         int r = min(n, i+a[i]);
60         int f = query(1, n, 1, i, r);
61         update(1, n, 1, i, f+1);
62     }
63     for(int i = 1; i <= n; i++)
64     {
65         int f = query(1, n, 1, 1, i);
66         update(1, n, 1, i, f+1);
67     }
68     for(int i = 1; i <= m; i++)
69     {
70         int x;
71         scanf("%d", &x);
72         int ans = query(1, n, 1, x, x);
73         printf("%d%c", ans, i==m?'\n':' ');
74         //printf("%d\n",ans);
75     }
76     return 0;
77 }
View Code

 

Guess you like

Origin www.cnblogs.com/Jony-English/p/12520099.html