Codeforces1301B. Motarack's Birthday
题意是说给你一串数组,其中-1代表未知,求相邻两个数之差的绝对值最小,-1可以由k赋值,先考虑-1的情况,把k解出来,转换一下,就是绝对值之差最小情况,|k-a|,|k-b|,|k-c|,要使最大的最小,也就是在两个端点找,|k-min|,|k-max|,由数形结合可知,当k为min与max的中点时最小,由此算出k再扫一遍数组即可
#include<bits/stdc++.h> using namespace std; #define lowbit(x) ((x)&(-x)) typedef long long LL; const int maxm = 1e5+5; const int INF = 0x3f3f3f3f; int buf[maxm], n; void run_case() { cin >> n; int Max = -1, Min = INF; for(int i = 0; i < n; ++i) cin >> buf[i]; for(int i = 0; i < n; ++i) { if(buf[i] == -1) { if(i > 0 && buf[i-1] != -1) Min = min(Min, buf[i-1]), Max = max(Max, buf[i-1]); if(i < n-1 && buf[i+1] != -1) Min = min(Min, buf[i+1]), Max = max(Max, buf[i+1]); } } int mid = (Max+Min)>>1, ans = 0; for(int i = 0; i < n-1; ++i) { int a = buf[i], b = buf[i+1]; if(a == -1) a = mid; if(b == -1) b = mid; ans = max(ans, abs(a-b)); } cout << ans << " " << mid << "\n"; } int main() { ios::sync_with_stdio(false), cin.tie(0); //cout.setf(ios_base::showpoint);cout.precision(10); int t; cin >> t; while(t--) run_case(); cout.flush(); return 0; }