1706C. Qpwoeirut And The CityCodeforces Round #809 (Div. 2)

C. Qpwoeirut And The City

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Qpwoeirut has taken up architecture and ambitiously decided to remodel his city.

Qpwoeirut's city can be described as a row of nn buildings, the ii-th (1≤i≤n1≤i≤n) of which is hihi floors high. You can assume that the height of every floor in this problem is equal. Therefore, building ii is taller than the building jj if and only if the number of floors hihi in building ii is larger than the number of floors hjhj in building jj.

Building ii is cool if it is taller than both building i−1i−1 and building i+1i+1 (and both of them exist). Note that neither the 11-st nor the nn-th building can be cool.

To remodel the city, Qpwoeirut needs to maximize the number of cool buildings. To do this, Qpwoeirut can build additional floors on top of any of the buildings to make them taller. Note that he cannot remove already existing floors.

Since building new floors is expensive, Qpwoeirut wants to minimize the number of floors he builds. Find the minimum number of floors Qpwoeirut needs to build in order to maximize the number of cool buildings.

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases.

The first line of each test case contains the single integer nn (3≤n≤1053≤n≤105) — the number of buildings in Qpwoeirut's city.

The second line of each test case contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤hi≤1091≤hi≤109) — the number of floors in each of the buildings of the city.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print a single integer — the minimum number of additional floors Qpwoeirut needs to build in order to maximize the number of cool buildings.

Example

input

Copy

6
3
2 1 2
5
1 2 1 4 3
6
3 1 4 5 5 2
8
4 2 1 3 5 3 6 1
6
1 10 1 1 10 1
8
1 10 11 1 10 11 10 1

output

Copy

2
0
3
3
0
4

Note

In the first test case, it is optimal for Qpwoeirut to make the second building cool by building 22 additional floors on top of it, making it taller than both of its adjacent buildings. The final heights of buildings will be [2,3–,2][2,3_,2].

In the second test case, the number of cool buildings is already maximized, so Qpwoeirut does not need to do anything.

In the third test case, it is optimal for Qpwoeirut to make the third and fifth buildings cool by building 22 additional floors onto the third building and 11 additional floor onto the fifth building. The final heights of buildings will be [3,1,6–,5,6–,2][3,1,6_,5,6_,2].

It can be shown that it is impossible to make more than 22 of the buildings cool, or to make 22 buildings cool using fewer than 33 additional floors.

In the fourth test case, Qpwoeirut can either make the second building cool, or he can make the third building cool. Either way, he will be building 33 additional floors and maximizing the number of cool buildings. The final heights of buildings will be [4,2,4–,3,5,3,6,1][4,2,4_,3,5,3,6,1] or [4,5–,1,3,5,3,6,1][4,5_,1,3,5,3,6,1].

教会了我很多的一道题,有两种情况,将楼层总数分为奇数和偶数两种,奇数楼层方案唯一可以直接算,偶数楼层方案很多,可能出现连续两个楼层都不cool的情况。

#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 5, INF = 0x3f3f3f3f;
LL a[maxn], s1[maxn], s2[maxn];

int main() 
{
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i], s1[i] = s2[i] = 1e18;
        if (n & 1) 
        {
            LL res = 0;
            for (int i = 2; i <= n - 1; i += 2)
                res += max(0ll, max(a[i - 1], a[i + 1]) + 1 - a[i]);
            cout << res << '\n';
        }
        else //如果楼数为偶数,就可能出现连续的两栋楼都不cool
        {
            LL sum = 0;
            for (int i = 2; i <= n - 1; i += 2)//下标全为偶数的前缀和从前往后扫描,为枚举组合做铺垫。 
            {
                sum += max(0LL, max(a[i - 1], a[i + 1]) + 1 - a[i]);
                s1[i] = sum;
            }
            sum = 0;
            for (int i = n - 1; i >= 2; i -= 2) //下标全为奇数的前缀和,从后往前扫描
            {
                sum += max(0LL, max(a[i - 1], a[i + 1]) + 1 - a[i]);
                s2[i] =  sum;
            }
            LL ans = min(s1[n - 2], s2[3]);//假设下标全为偶数和下标全为奇数需要建设的楼层比较
            for (int i = 2; i + 3 <= n; i++)
                ans = min(ans, s1[i] + s2[i + 3]);//开始枚举所有下标偶数和奇数的组合。
            cout << ans << '\n';
           
        }
    }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值