ARC072E Alice in linear land < DP >

Problem

Alice in linear land


Statement

Alice lives on a line. Today, she will travel to some place in a mysterious vehicle. Initially, the distance between Alice and her destination is . When she input a number to the vehicle, it will travel in the direction of the destination by a distance of if this move would shorten the distance between the vehicle and the destination, and it will stay at its position otherwise. Note that the vehicle may go past the destination when the distance between the vehicle and the destination is less than .
Alice made a list of numbers. The number in this list is . She will insert these numbers to the vehicle one by one.
However, a mischievous witch appeared. She is thinking of rewriting one number in the list so that Alice will not reach the destination after moves.
She has plans to do this, as follows:
Rewrite only the number in the list with some integer so that Alice will not reach the destination.
Write a program to determine whether each plan is feasible.

Constraints






and each are integers.

Input

Input is given from Standard Input in the following format:



Output

Print lines. The line should contain YES if the plan is feasible, and NO otherwise.

Sample

Input #1

1
2
3
4
4 10
3 4 3 3
2
4 3

Output #1

1
NO

Explanation #1
For the first plan, Alice will already arrive at the destination by the first three moves, and therefore the answer is NO. For the second plan, rewriting the third number in the list with will prevent Alice from reaching the destination as shown in the following figure, and thus the answer is YES.




Input #2

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

Output #2

1
2
3
4
5
YES
YES
YES
YES
YES

Explanation #2
Alice will not reach the destination as it is, and therefore all the plans are feasible.
Input #3

1
2
3
4
6 15
4 3 5 4 2 1
6
1 2 3 4 5 6

Output #3

1
2
3
4
5
6
NO
NO
YES
NO
NO
YES

标签:DP

Translation

有一个无限长的数轴,你现在在 处,有 条指令,每条指令为一个整数 。顺次处理每条指令,如果你当前在 处,你会走到 处。有 条询问,对于每条询问,回答能否通过改变 的值使得最后不能到达

Solution

精妙的做法,有点类似构造,又有点像

首先处理出数组 ,其中 表示在没有更改指令的情况下,处理前 条指令后的位置。那么如果能够修改 ,我们可以在处理完 后到达 中的所有位置。我们需要找到一个数组 ,其中 表示修改并处理完 后,至少要在哪个位置才能使处理完后面的指令后不能到达 。显然,如果 ,一定能找到可行方案,否则一定不能。

考虑如何得到 。我们倒序计算 。首先,显然有 。不难发现 一定是递减的,于是倒序计算时我们用前面的值作为基础值来处理出现在处理到的 。对于 不小于 ,如果 ,那么从 位置进行 指令一定不会走到更小的点上,因此 可以取到其最小值 ;否则,需要使其操作 后不小于 ,因此

处理出 后判断即可。时间复杂度

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <bits/stdc++.h>
#define MAX_N 500000
using namespace std;
template <class T> inline void read(T &x) {
x = 0; int c = getchar(), f = 1;
for (; !isdigit(c); c = getchar()) if (c == 45) f = -1;
for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0');
}
int n, d[MAX_N+5], a[MAX_N+5], b[MAX_N+5];
int main() {
read(n), read(a[0]), b[n] = 1;
for (int i = 1; i <= n; i++) read(d[i]);
for (int i = 1; i <= n; i++)
a[i] = min(a[i-1], abs(a[i-1]-d[i]));
for (int i = n-1; i; i--)
if (b[i+1] <= d[i+1]/2) b[i] = b[i+1];
else b[i] = b[i+1]+d[i+1];
int m; read(m);
while (m--) {
int p; read(p);
if (b[p] <= a[p-1]) puts("YES");
else puts("NO");
}
return 0;
}
------------- Thanks For Reading -------------
0%