#P4505. [CTSC2013] 组合子逻辑
[CTSC2013] 组合子逻辑
Description
Combinatory logic is a symbolic system invented by Moses Schönfinkel and Haskell Curry to eliminate the need for variables in mathematical logic. This problem considers a combinator system that slightly differs from the one in the real world.
A combinator term is one of the following forms:
Here denotes a primitive function, and and are combinator terms (they may be the same). Any expression not in the above forms is not a combinator term.
We define the number of arguments of a combinator term , denoted , as follows:
equals the number of arguments (arity) of the primitive function .
.
In this problem, we use a positive integer to denote both a primitive function and the arity of that primitive function.
For a combinator term , if of and of all combinator subterms it contains are positive integers, we say that is in normal form.
We often use a simplified notation for combinator terms: if a combinator term contains a consecutive subsequence (where ), where each denotes a combinator term (which may itself be in simplified notation), then we replace that part by while keeping the rest unchanged, obtaining a simplified representation of . A combinator term may be simplified in this way multiple times.
Given a sequence of primitive functions, determine the minimum number of pairs of parentheses that must be added to make the expression a simplified representation of a normal form (i.e., it satisfies the normal-form property). If it is impossible to obtain a simplified representation of a normal form no matter how you add parentheses, output .
Input Format
The first line contains a positive integer , the number of queries.
Then follow lines.
Line contains a positive integer , the number of primitive functions in the sequence of the -th query.
Line contains positive integers, where the -th integer denotes the -th primitive function in the sequence.
Output Format
Output lines, each containing one integer, the answer for the corresponding query.
2
5
3 2 1 3 2
5
1 1 1 1 1
3
-1
Hint
【Sample Explanation】
For the first query: an optimal plan is (3 (2 1) (3 2)). One can prove that there is no plan that uses fewer pairs of parentheses.
For the second query: it is easy to prove that no valid plan exists.
【Constraints】
Let denote the sum of all in the input.
| 测试点编号 | 规模 |
|---|---|
| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
Translated by ChatGPT 5
京公网安备 11011102002149号