#C3326. Minimum Operations to Merge Adjacent Characters

    ID: 46741 Type: Default 1000ms 256MiB

Minimum Operations to Merge Adjacent Characters

Minimum Operations to Merge Adjacent Characters

Given several test cases, each test case consists of an integer (N) and a list of (N) integers representing the power levels of characters arranged in a line. Two adjacent characters can be merged if the absolute difference of their power levels is exactly (1). Each merge operation reduces the number of characters by one. The goal is to determine the minimum number of merge operations required to reduce the entire line to a single character.

Note: If no adjacent pair with power difference (1) exists, no merges can be performed. Merging is performed greedily by continuously merging adjacent eligible pairs in the order they appear.

inputFormat

The input is read from standard input. The first line contains an integer (T), the number of test cases. For each test case, the first line contains an integer (N), the number of characters. The second line contains (N) space-separated integers representing the power levels.

outputFormat

For each test case, output a single integer on a new line that denotes the minimum number of merge operations required to reduce the character line into one character.## sample

3
4
1 2 3 4
3
2 3 4
5
1 3 5 7 9
3

2 0

</p>