题意:
给定一个长度为n的数组,求出是否存在一个数x使得,由|ai-x|构成的数组bi满足(bi <= bi+1)
思路:
对于任意两个数a1,a2求|ai-x|有以下几种情况
1. x < (a1,a2)/2:
新数组 b1,b2 单调性与a1,a2单调性相同
2.x = (a1,a2)/2:
新数组 b1=b2
3.x > (a1,a2)/2:
新数组 b1,b2 单调性与a1,a2单调性相反
代码:
#include<iostream> #include<cstdio> #include<cmath> using namespace std; typedef long long LL; const LL N = 2*1e5+10, INF = 0x3f3f3f3f; LL a[N]; int main() { int t; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%lld", &a[i]); } LL l = 0, r = INF; for(int i = 1; i < n; i++) { LL x = 0, y = INF; if(a[i] > a[i-1]) y = (a[i]+a[i-1])/2; else if(a[i] < a[i-1]) x=(a[i]+a[i-1]+1)/2; l = max(l, x),r = min(r, y); } if(l > r) printf("-1\n"); else printf("%lld\n", l); } return 0; }
标签:Sorting,b2,int,LL,a1,a2,数组,CF1772D,Absolute From: https://www.cnblogs.com/lys-blogs123/p/17088514.html