Bayraktar
Limits: 3 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk is a Bayraktar operator. Reconnaissance reported that an occupier convoy of \(n\) trucks is planning to blow up bridges to slow down the advance of the Ukrainian army. It is known that the \(i\)-th truck is currently at coordinate \(a_i\) and contains \(b_i\) of explosives. If the \(i\)-th truck explodes, all trucks in the range \([a_i - b_i, a_i + b_i]\) will also explode. Unfortunately, the Bayraktar operated by Zenyk has only one low-power missile left, and therefore it destroys only the target it hits. Since Zenyk controls the Bayraktar very skillfully, he can hit any truck with the missile. Help Zenyk and tell him the maximum number of trucks he can blow up.
Input
The first line gives a single integer \(n\) — the number of trucks with explosives.
The second line gives \(n\) integers \(a_i\) — the coordinate of the \(i\)-th truck.
The third line gives \(n\) integers \(b_i\) — the amount of explosives in the \(i\)-th truck.
Output
In a single line, output a single integer — the maximum number of trucks that Zenyk can blow up.
Constraints
\(1 \le n \le 10^{5}\),
\(0 \le a_i, b_i \le 10^{9}\), all \(a_i\) are distinct.
Problem scoring consists of the following blocks:
1 point — the first example from the problem statement,
1 point — the second example from the problem statement,
6 points — a block of tests in which \(1 \le n \le 100\),
7 points — a block of tests in which \(1 \le n \le 3000\),
10 points — a block of tests in which \(1 \le n \le 10^{5}\),
You will receive points for a block only if you give the correct answer to all tests in the block.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 5 1 5 7 8 9 3 2 1 2 1 | 4 |
| Input (stdin) | Output (stdout) |
|---|---|
| 6 3 2 1 7 5 9 1 0 1 2 0 0 | 3 |
Notes
In the first example, it is best to blow up the second truck, which will directly blow up the third, which in turn will blow up the fourth, which will blow up the fifth. As a result, 4 trucks will be blown up.
In the second example, it is best to blow up the fourth truck, located at coordinate 7, which will directly blow up the fifth and sixth trucks. As a result, 3 trucks will be blown up.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Bayraktar - Editorial
For convenience, sort the trucks by coordinate.
To get the points for the two easiest test groups, we had to try every truck to shoot at and reproduce the process of explosions. For example, we can maintain a range \([l, r]\) such that all trucks in it will explode. However, this range can contain trucks we have not considered yet, and such trucks can update the values of \(l\) and \(r\). In the easier group we could iterate over all trucks and check whether they are in the range and not yet considered. For the second group, the search for unconsidered trucks has to be faster. For example, we can keep an ordered set or use the two pointers technique.
To solve the problem, note that if the explosion segment of a truck \([a_j - b_j, a_j + b_j]\) is nested in the explosion segment of another truck \([a_i - b_i, a_i + b_i]\), there is no point in shooting at the \(j\)-th truck, since the \(i\)-th one blows up at least as many trucks. If we do not try to blow up trucks like the \(j\)-th one, the explosion spreads to the left and to the right independently. That is, if the detonation reaches a truck on the right, the explosion from it does not spread further left than before. So we need to count separately how many trucks we blow up on the left and on the right, as if the explosions were independent. That is, the explosions of the \(i\)-th truck happen on the ranges \([a_i - b_i, a_i]\) and \([a_i, a_i + b_i]\) for the blast waves to the left and to the right, respectively. To solve these subproblems, we use dynamic programming. Let \(L[i]\) be the number of the leftmost truck the blast wave to the left can blow up if the \(i\)-th truck explodes. Similarly, \(R[i]\) is the number of the rightmost truck for the blast wave to the right. To compute \(L[i]\), we need the smallest value \(L[j]\) such that \(a_i - b_i \le a_j \le a_i\). Similarly, to compute \(R[i]\), we need the largest value \(R[j]\) such that \(a_i \le a_j \le a_i + b_i\). These values can be found with data structures that support range minimum queries and point updates, for example a segment tree. With these values, we know that blowing up the \(i\)-th truck gives the answer \(R[i] - L[i] + 1\). The answer is the maximum of these values.
Note that if the explosions cannot be considered independently, that is, for trucks whose blast wave is nested, the value \(R[i] - L[i] + 1\) can be less than the number of trucks that explode if the \(i\)-th one is blown up first. However, we have shown that shooting at such trucks is not optimal, which means we can ignore this.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|