You want to build a temple for snakes. The temple will be built on a mountain range, which can be thought of as n blocks, where height of i-th block is given by hi. The temple will be made on a consecutive section of the blocks and its height should start from 1 and increase by exactly 1 each time till some height and then decrease by exactly 1 each time to height 1, i.e. a consecutive section of 1, 2, 3, .. x-1, x, x-1, x-2, .., 1 can correspond to a temple. Also, heights of all the blocks other than of the temple should have zero height, so that the temple is visible to people who view it from the left side or right side.
You want to construct a temple. For that, you can reduce the heights of some of the blocks. In a single operation, you can reduce the height of a block by 1 unit. Find out minimum number of operations required to build a temple.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains an integer n.
The next line contains n integers, where the i-th integer denotes hi
Output
For each test case, output a new line with an integer corresponding to the answer of that testcase.
Constraints
- 1 ≤ T ≤ 10
- 2 ≤ n ≤ 105
- 1 ≤ hi ≤ 109
Example
Input 3 3 1 2 1 4 1 1 2 1 5 1 2 6 2 1 Output 0 1 3
Explanation
Example 1. The entire mountain range is already a temple. So, there is no need to make any operation.
Example 2. If you reduce the height of the first block to 0. You get 0 1 2 1. The blocks 1, 2, 1 form a temple. So, the answer is 1.
Example 3. One possible temple can be 1 2 3 2 1. It requires 3 operations to build. This is the minimum amount you have to spend in order to build a temple.
题意:你想要为众蛇建造一座神殿。神殿将建在群山之中,可以将其视作 n 个块,其中第 i 个的高度为 hi。神殿会建造在连续若干个块上,要求这些块的高度从 1 开始逐渐增加到某个高度,然后再逐渐减小到 1,即,高度是形如 1, 2, 3, . . . , x − 1, x, x − 1, x − 2, . . . , 1 的序列。除了要建造神殿的块之外,其他块的高度都应该是 0,以便人们从左右两侧看到神殿。为了达成这一目标,你可以降低块的高度。一次操作中,你可以选择一块,并令其高度减小1。请求出最少需要进行多少次操作。# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5;
int t, n, a[maxn+3];
bool judge(LL x)
{
int cnt = 1, flag = 1;
for(int i=1; i<=n; ++i)
{
if(i == n-x+1 && flag == 1 && cnt < x) return false;
if(a[i] >= cnt)
{
if(cnt == x) flag = -1;
cnt += flag;
if(cnt == 0 && flag == -1) return true;
}
else
cnt = a[i]+1,flag = 1;
}
return false;
}
int main()
{
scanf("%d",&t);
while(t--)
{
LL sum = 0;
int imax = 0;
scanf("%d",&n);
for(int i=1; i<=n; ++i)
{
scanf("%d",&a[i]);
imax= max(imax, a[i]);
sum += a[i];
}
LL l=1, r=imax;
while(l<=r)
{
LL mid = l+r>>1;
if(judge(mid)) l = mid+1;
else r = mid-1;
}
printf("%lld\n",sum-(1+r)*r+r);
}
return 0;
}
# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5+3;
LL a[maxn], dpl[maxn]={0}, dpr[maxn]={0};
int main()
{
int t, n;
scanf("%d",&t);
while(t--)
{
LL imax = 0;
LL sum = 0;
scanf("%d",&n);
dpr[n+1] = 0;
for(int i=1; i<=n; ++i)
{
scanf("%lld",&a[i]);
sum += a[i];
dpl[i] = min(dpl[i-1]+1LL, a[i]);
}
for(int i=n; i>=1; --i)
{
dpr[i] = min(dpr[i+1]+1LL, a[i]);
imax = max(imax, min(dpl[i], dpr[i]));
}
printf("%lld\n",sum - imax*imax);
}
return 0;
}