Problem list
Log in
#usaco0584 · USACO 2023-2024 December Silver

Target Practice

Platinum IV 2.0 s 256 MB

Bessie is a robovine, also known as a cowborg. She is on a number line trying to shoot a series of $T$ $(1 \leq T \leq 10^5)$ targets located at distinct positions. Bessie starts at position $0$ and follows a string of $C$ $(1 \leq C \leq 10^5)$ commands, each one of L, F, or R:

  • L: Bessie moves one unit to the left.
  • R: Bessie moves one unit to the right.
  • F: Bessie fires. If there is a target at Bessie's current position, it is hit and destroyed, and cannot be hit again.

If you are allowed to change up to one command in the string to a different command before Bessie starts following it, what is the maximum number of targets that Bessie can hit?

Input

The first line contains $T$ and $C$.

The next line contains the locations of the $T$ targets, distinct integers in the range $[-C,C]$.

The next line contains the command string of length $C$, containing only the characters F, L, and R.

Output

Print the maximum number of targets that Bessie can hit after changing up to one command in the string.

Constraints

SCORING

  • Inputs 4-6: $T,C \le 1000$
  • Inputs 7-15: No additional constraints.

Example 1

Input
3 7
0 -1 1
LFFRFRR
Output
3

If you make no changes to the string, Bessie will hit two targets:

Command | Position | Total Targets Hit
--------+----------+-------------------
Start   |  0       | 0
L       | -1       | 0
F       | -1       | 1
F       | -1       | 1 (can't destroy target more than once)
R       |  0       | 1
F       |  0       | 2
R       |  1       | 2
R       |  2       | 2

If you change the last command from R to F, Bessie will hit all three targets:

Command | Position | Total Targets Hit
--------+----------+-------------------
Start   |  0       | 0
L       | -1       | 0
F       | -1       | 1
F       | -1       | 1 (can't destroy target more than once)
R       |  0       | 1
F       |  0       | 2
R       |  1       | 2
F       |  1       | 3

Example 2

Input
1 5
0
FFFFF
Output
1

If the commands are left unchanged, the only target at 0 will be destroyed. Since a target cannot be destroyed multiple times, the answer is 1.

Example 3

Input
5 6
1 2 3 4 5
FFRFRF
Output
3

Problem credits: Suhas Nagar

Source
USACO 2023-2024 December Silver
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code