#P11838. Bessie’s Simple Programming Challenge
Bessie’s Simple Programming Challenge
Bessie’s Simple Programming Challenge
Bessie is learning to program in a simple programming language. In this language, a program is a non‐empty sequence of statements. A statement can be either a PRINT c
statement, which appends the integer c to the output, or a loop statement that starts with REP o
, followed by a program and terminated by END
, where o (\(\ge 1\)) is the number of repetitions.
When executed, the program processes its statements sequentially. The PRINT c
statement appends c to the output sequence, and the REP o
statement executes its inner program exactly o times. For example, the following program:
REP 3 PRINT 1 REP 2 PRINT 2 END END
produces the output sequence \([1,2,2,1,2,2,1,2,2]\).
Bessie wants to produce an output sequence containing N (\(1 \le N \le 100\)) positive integers. Elsie challenges her to write a program that uses no more than K (\(1 \le K \le 3\)) PRINT
statements in the source code. Note that although an arbitrary number of REP
statements may be used, every printed integer (when the program is executed) does not exceed K.
You are given T (\(1 \le T \le 100\)) independent test cases. For each test case, determine whether Bessie can write a program, using at most K PRINT
statements, that outputs the given sequence.
inputFormat
The first line contains an integer T, the number of test cases. Each test case consists of two lines. The first line contains two integers N and K, representing the length of the sequence and the maximum number of PRINT
statements allowed, respectively. The second line contains N positive integers (each \(\le K\)), representing the desired output sequence.
outputFormat
For each test case, output a single line: YES
if Bessie can write a program using at most K PRINT
statements to produce the given sequence; otherwise, output NO
.
sample
4
9 2
1 2 2 1 2 2 1 2 2
3 1
1 1 1
3 1
1 2 1
4 2
1 1 2 2
YES
YES
NO
NO
</p>