Codeforces Round #619 (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≤109) 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 |ai−ai+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≤104) — 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.

样例:

输入:
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

输出:
1 11
5 35
3 6
0 42
0 0
1 2
3 4

思路:
找出-1附近的数值的最大值和最小值,然后d = (minn + maxx) / 2,将其带入所有-1值,求出差值即可。

AC代码:

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int a[maxn];
int MAX(int a, int b) {
	return a > b ? a : b;
}
int MIN(int a, int b) {
	return a < b ? a : b;
}
void solve(int n) {
	int maxx = inf;
	int minn = -inf;
	for (int i = 0; i < n; i++) {
		if (-1 == a[i]) {
			if (i != 0 && a[i - 1] != -1) {
				minn = MAX(minn, a[i - 1]);
				maxx = MIN(maxx, a[i - 1]);
 
			}
			if (a[i + 1] != -1 && i + 1 != n) {
				minn = MAX(minn, a[i + 1]);
				maxx = MIN(maxx, a[i + 1]);
 
			}
		}
	}
	if (minn == -inf || maxx == inf) {
		printf("0 0\n");
		return;
	}
	else {
		int ans = -inf;
		int d = (minn + maxx) / 2;
		for (int i = 1; i < n; i++) {
			if (a[i - 1] == -1)
				a[i - 1] = d;
			if (a[i] == -1)
				a[i] = d;
			ans = MAX(ans, abs(a[i] - a[i - 1]));
		}
		printf("%d %d\n", ans, d);
	}
}
int main()
{
	int t; scanf("%d", &t);
	for (int i = 0; i < t; i++) {
		int n; scanf("%d", &n);
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		solve(n);
	}
	return 0;
}
发布了40 篇原创文章 · 获赞 6 · 访问量 1400

猜你喜欢

转载自blog.csdn.net/qq_43321732/article/details/104319511