Motarack's Birthday CodeForces - 1301B(思维)

Dark is going to attend Motarack’s birthday. Dark decided that the gift he is going to give to Motarack is an array aa of nn non-negative integers.

Dark created that array 10001000 years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn’t have much time so he wants to choose an integer kk (0≤k≤1090≤k≤109) and replaces all missing elements in the array aa with kk.

Let mm be the maximum absolute difference between all adjacent elements (i.e. the maximum value of |ai−ai+1||ai−ai+1| for all 1≤i≤n−11≤i≤n−1) in the array aa after Dark replaces all missing elements with kk.

Dark should choose an integer kk so that mm is minimized. Can you help him?

Input
The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains one integer nn (2≤n≤1052≤n≤105) — the size of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−1≤ai≤109−1≤ai≤109). If ai=−1ai=−1, then the ii-th integer is missing. It is guaranteed that at least one integer is missing in every test case.

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

Output
Print the answers for each test case in the following format:

You should print two integers, the minimum possible value of mm and an integer kk (0≤k≤1090≤k≤109) that makes the maximum absolute difference between adjacent elements in the array aa equal to mm.

Make sure that after replacing all the missing elements with kk, the maximum absolute difference between adjacent elements becomes mm.

If there is more than one possible kk, you can print any of them.

Example
Input
7
5
-1 10 -1 12 -1
5
-1 40 35 -1 35
6
-1 -1 9 -1 3 -1
2
-1 -1
2
0 -1
4
1 -1 3 -1
7
1 -1 7 5 2 -1 5
Output
1 11
5 35
3 6
0 42
0 0
1 2
3 4
Note
In the first test case after replacing all missing elements with 1111 the array becomes [11,10,11,12,11][11,10,11,12,11]. The absolute difference between any adjacent elements is 11. It is impossible to choose a value of kk, such that the absolute difference between any adjacent element will be ≤0≤0. So, the answer is 11.

In the third test case after replacing all missing elements with 66 the array becomes [6,6,9,6,3,6][6,6,9,6,3,6].

|a1−a2|=|6−6|=0|a1−a2|=|6−6|=0;
|a2−a3|=|6−9|=3|a2−a3|=|6−9|=3;
|a3−a4|=|9−6|=3|a3−a4|=|9−6|=3;
|a4−a5|=|6−3|=3|a4−a5|=|6−3|=3;
|a5−a6|=|3−6|=3|a5−a6|=|3−6|=3.
So, the maximum difference between any adjacent elements is 33.
思路:对于每一个靠着-1的数字,都是要用来比较的,我们要使差值绝对值尽可能低,但是我们想想,其实就保证和最大值或者最小值的差值绝对值尽可能低就行了(因为其余的数字差值绝对值一定在这之间),这样就求出最大值与最小值的和的一半,然后求最大差值就可以了。
代码如下:

#include<bits/stdc++.h>
using namespace std;

const int maxx=1e5+100;
int a[maxx];
vector<int>p;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
      p.clear();
      int n;cin>>n;
      for(int i=1;i<=n;i++) cin>>a[i];
      for(int i=1;i<=n;i++) if(a[i]!=-1 && (a[i-1]==-1 || a[i+1]==-1)) p.push_back(a[i]);
      sort(p.begin(),p.end());
      int m,k;
      if(p.empty()) k=0;
      else k=(p[0]+p[p.size()-1])/2;
      m=0;
      for(int i=1;i<=n;i++){
          if(a[i]==-1) a[i]=k;
          if(i>=2) m=max(m,abs(a[i]-a[i-1]));
      }
      cout<<m<<" "<<k<<endl;
    }
    return 0;
}

努力加油a啊,(o)/~

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值