Codeforces Round #619 (Div. 2) B.Motarack's Birthday

Codeforces Round #619 (Div. 2) B.Motarack’s Birthday

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

Dark created that array 1000 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 k ( 0 k 1 0 9 ) k (0≤k≤10^9) and replaces all missing elements in the array a with k.

Let m be the maximum absolute difference between all adjacent elements (i.e. the maximum value of a i a i + 1 |a_i−a_{i+1}| for all 1≤i≤n−1) in the array a after Dark replaces all missing elements with k.

Dark should choose an integer k so that m is minimized. Can you help him?

Input

The input consists of multiple test cases. The first line contains a single integer t ( 1 t 1 0 4 ) (1≤t≤10^4) — the number of test cases. The description of the test cases follows.

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

The second line of each test case contains n integers a1,a2,…,an (−1≤ai≤109). If ai=−1, then the i-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 n for all test cases does not exceed 4⋅105.

Output

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

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

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

If there is more than one possible k, 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

这道题找到最优解是最大值和最小值的一半,但是真的没想到最大值和最小值只针对-1周围的数,如果一个数两边都是非负数,则不用考虑,/(ㄒoㄒ)/~~

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
int main() {
	int t,n;
	ll a[N];
	cin>>t;
	while(t--){
        scanf("%d",&n);
        ll maxnum=-1e9,minnum=1e9;
        for(int i=0;i<n;i++) {
            scanf("%lld",&a[i]);
        }
        for(int i=0;i<n;i++) {
            if(i>0 && a[i]==-1 && a[i-1]!=-1)
                maxnum=max(maxnum,a[i-1]),minnum=min(minnum,a[i-1]);
            if(i<n-1 && a[i]==-1 && a[i+1]!=-1)
                maxnum=max(maxnum,a[i+1]),minnum=min(minnum,a[i+1]);
        }
        ll k=(minnum+maxnum)/2,m=0;
        for(int i=0;i<n;i++){
            if(a[i]<0) a[i]=k;
            if(i>0) m=max(m,abs(a[i]-a[i-1]));
        }
        cout<<m<<" "<<k<<endl;
	}
	return 0;
}
发布了296 篇原创文章 · 获赞 15 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_43765333/article/details/104395165