#P13120. [GCJ 2019 #3] Pancake Pyramid
[GCJ 2019 #3] Pancake Pyramid
题目描述
You have just finished cooking for some diners at the Infinite House of Pancakes. There are stacks of pancakes in all, and you have arranged them in a line, such that the i-th stack from the left (counting starting from 1) has pancakes.
Your supervisor was about to bring out the stacks to the customers, but then it occurred to her that a picture of the stacks might make for a good advertisement. However, she is worried that there might be too many stacks, so she intends to remove the leftmost stacks and the rightmost stacks, where and are nonnegative integers such that . (Notice that at least 3 stacks of pancakes will remain after the removal.)
Your supervisor also thinks the remaining stacks will look aesthetically pleasing if they have the pyramid property. A sequence of stacks of heights $\mathbf{H}_1, \mathbf{H}_2, \ldots, \mathbf{H}_\mathbf{N}$ has the pyramid property if there exists an integer () such that $\mathbf{H}_1 \leq \mathbf{H}_2 \leq \ldots \leq \mathbf{H}_{\mathbf{j}-1} \leq \mathbf{H}_\mathbf{j}$ and $\mathbf{H}_\mathbf{j} \geq \mathbf{H}_{\mathbf{j}+1} \geq \ldots \geq \mathbf{H}_{\mathbf{N}-1} \geq \mathbf{H}_\mathbf{N}$. (It is possible that this sequence might not look much like a typical "pyramid" — a group of stacks of the same size has the pyramid property, and so does a group in which the stack heights are nondecreasing from left to right, among other examples.)
Note that the sequence of stacks remaining after your supervisor removes the leftmost and rightmost stacks might not yet have the pyramid property... but you can fix that by adding pancakes to one or more of the stacks! The pyramidification cost of a sequence of stacks is the minimum total number of pancakes that must be added to stacks to give the sequence the pyramid property.
While your manager is carefully deciding which values of and to choose, you have started to wonder what the sum of the pyramidification costs over all valid choices of and is. Compute this sum, modulo the prime ().
输入格式
The first line of the input gives the number of test cases, . test cases follow. Each begins with one line containing one integer : the number of stacks of pancakes. Then, there is one more line containing integers $\mathbf{P}_1, \mathbf{P}_2, \ldots, \mathbf{P}_\mathbf{S}$. The i-th of these is the number of pancakes in the i-th stack from the left.
输出格式
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is the sum of the pyramidification costs over all valid choices of and , modulo the prime ().
3
3
2 1 2
5
1 6 2 5 7
4
1000000000 1 1 1000000000
Case #1: 1
Case #2: 16
Case #3: 999999991
提示
Sample Explanation
In Sample Case #1, your supervisor must choose and , so that is the only scenario you need to consider. The optimal strategy for that scenario is to add a single pancake to the middle stack. Although the resulting sequence of stacks looks flat, notice that it has the pyramid property; in fact, any index will work as the value.
In Sample Case #2, here are all possible choices of and , the corresponding remaining stacks, and what you should do in each scenario.
- , : . The optimal solution is to add four pancakes to the third stack and one pancake to the fourth stack. Then we have , which has the pyramid property with .
- , : . The optimal solution is to add three pancakes to the third stack. Then we have , which has the pyramid property with .
- , : . This already has the pyramid property with .
- , : . The optimal solution is to add four pancakes to the second stack and one pancake to the third stack. Then we have , which has the pyramid property with .
- , : . The optimal solution is to add three pancakes to the second stack. Then we have , which has the pyramid property with .
- , : . This already has the pyramid property with .
So the answer is modulo , which is .
In Sample Case #3, we only need to add extra pancakes to create the pyramid property when and . In that case, it is optimal to add pancakes to each of the second and third stacks. (We hope the diners are hungry!) So the answer is modulo .
Limits
- .
- , for all .
Test set 1 (5 Pts, Visible)
- , for up to 20 test cases.
- , for all remaining cases.
Test set 2 (17 Pts, Hidden)
- , for up to 1 test case.
- , for up to 3 test cases.
- , for all remaining cases.