#P16218. [ECUSTPC 2025] 钟塔
[ECUSTPC 2025] 钟塔
Problem Description
Maddy is preparing to rebuild a clock tower.
Maddy has a sequence of length . Now she wants to transform a sequence of length that is initially all 0 into using the minimum number of operations. Each operation works as follows:
- Maddy chooses a position () and a direction .
- If , then for all , set to .
- If , then for all , set to .
- Note that each operation completely overwrites the corresponding interval.
Please help her find the minimum number of operations needed. If it is impossible to achieve the goal using the operations above, report that there is no solution.
Input Format
The first line contains an integer (), denoting the number of test cases.
For each test case, the first line contains an integer (), denoting the length of the sequence.
The next line contains integers (), denoting the target sequence that Maddy wants to construct.
It is guaranteed that over all test cases.
Output Format
For each test case, if the goal can be achieved using the operations above, output one integer on a line, denoting the minimum number of operations Maddy needs.
Otherwise, output one integer on a line.
4
6
1 2 1 1 2 3
3
3 3 3
6
1 1 1 1 1 1
11
1 2 1 3 4 1 2 1 1 2 3
3
-1
6
6
Hint
Explanation for Sample 1
For the 1st sample, the operations can be described as:
- Choose , , then .
- Choose , , then .
- Choose , , then .
It is easy to see that this is a minimum sequence of operations.
For the 2nd sample, it is easy to see that no matter what operations Maddy performs, can never become 3.
Translated by ChatGPT 5