CF1534B Histogram Ugliness-Codeforces LATOKEN Round 1 (Div. 1 + Div. 2)

Problem - 1534B - Codeforces

B. Histogram Ugliness

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Little Dormi received a histogram with nn bars of height a1,a2,…,ana1,a2,…,an for Christmas. However, the more he played with his new histogram, the more he realized its imperfections, so today he wanted to modify it to his liking.

To modify the histogram, Little Dormi is able to perform the following operation an arbitrary number of times:

  • Select an index ii (1≤i≤n1≤i≤n) where ai>0ai>0, and assign ai:=ai−1ai:=ai−1.

Little Dormi defines the ugliness score of his histogram (after performing some number of operations) as the sum of the vertical length of its outline and the number of operations he performed on it. And to make the histogram as perfect as possible, he would like to minimize the ugliness score after modifying it with some number of operations.

However, as his histogram is very large, Little Dormi is having trouble minimizing the ugliness score, so as Little Dormi's older brother, help him find the minimal ugliness.

Consider the following example where the histogram has 44 columns of heights 4,8,9,64,8,9,6:

The blue region represents the histogram, and the red lines represent the vertical portion of the outline. Currently, the vertical length of the outline is 4+4+1+3+6=184+4+1+3+6=18, so if Little Dormi does not modify the histogram at all, the ugliness would be 1818.

However, Little Dormi can apply the operation once on column 22 and twice on column 33, resulting in a histogram with heights 4,7,7,64,7,7,6:

Now, as the total vertical length of the outline (red lines) is 4+3+1+6=144+3+1+6=14, the ugliness is 14+3=1714+3=17 dollars. It can be proven that this is optimal.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤4⋅1051≤n≤4⋅105).

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109).

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

Output

For each test case output one integer, the minimal ugliness Little Dormi can achieve with the histogram in that test case.

Example

input

Copy

2
4
4 8 9 6
6
2 1 7 4 0 0

output

Copy

17
12

Note

Example 11 is the example described in the statement.

The initial histogram for example 22 is given below:

The ugliness is currently 2+1+6+3+4=162+1+6+3+4=16.

By applying the operation once on column 11, six times on column 33, and three times on column 44, we can end up with a histogram with heights 1,1,1,1,0,01,1,1,1,0,0:

The vertical length of the outline is now 1+1=21+1=2 and Little Dormi made 1+6+3=101+6+3=10 operations, so the final ugliness is 2+10=122+10=12, which can be proven to be optimal.

---------------------------------------------------------------------------------------------------------------------------------

首先,题目要求的是丑陋度与操作次数的最小值,我们设为cnt,刚开始肯定是等于总丑陋值。

我们对裸露在外的山峰进行操作时,会增加一次操作数,减少两个丑陋值。所以我们先进行“去峰处理”,也就是说消去全部山峰。

 for(int i=1;i<=n;i++)
          {
              if(a[i]>a[i-1]&&a[i]>a[i+1])
                {
                    ans+=a[i]-max(a[i-1],a[i+1]);
                    a[i]=max(a[i-1],a[i+1]);
                }

                //cout<<a[i]<<" ";

          }

至于为什么要消到和两侧较高的为止,是因为再消的话会漏出来新的丑陋值,和操作次数抵消了。

然后我们再思考,还能不能再消呢?首先,要让总和减小,必须出现山峰,现在已经没有山峰了,那我们能不能通过消去部分来实现山峰的出现呢。例如三列方块,我们要让中间那列变成山峰,分类讨论各种情况,发现根本不可能再实现可以让答案变小的山峰 

# include<iostream>
# include<algorithm>
# include<iomanip>
# include<vector>
# include<map>
# include<math.h>
# include<cstring>

using namespace std;
typedef long long int ll;

int a[400000+10];

int main()
{
      int t;
      cin>>t;

      while(t--)
      {
          int n;
          cin>>n;

          for(int i=1;i<=n;i++)
          {
              cin>>a[i];
          }

          int ans=0;


          for(int i=1;i<=n;i++)
          {
              if(a[i]>a[i-1]&&a[i]>a[i+1])
                {
                    ans+=a[i]-max(a[i-1],a[i+1]);
                    a[i]=max(a[i-1],a[i+1]);
                }

                //cout<<a[i]<<" ";

          }

          for(int i=1;i<=n;i++)
          {
              if(a[i]>a[i-1]||a[i]>a[i+1])
                ans+=a[i]-min(a[i-1],a[i+1]);

          }



          cout<<ans<<'\n';
      }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值