【二分】bestcoder p1m2
模型的转化和二分check的细节挺不错的
题目分析
二分的check细节精巧
1 #include<bits/stdc++.h> 2 const int maxn = 300035; 3 4 int T,n,ans,mx,mn,l,r; 5 int a[maxn]; 6 7 int read() 8 { 9 char ch = getchar(); 10 int num = 0; 11 bool fl = 0; 12 for (; !isdigit(ch); ch = getchar()) 13 if (ch=='-') fl = 1; 14 for (; isdigit(ch); ch = getchar()) 15 num = (num<<1)+(num<<3)+ch-48; 16 if (fl) num = -num; 17 return num; 18 } 19 bool check(int x) 20 { 21 long long cnt = 0; 22 for (int i=1; i<=n; i++) 23 if (a[i] <= x) cnt += x-a[i]; 24 else cnt -= (a[i]-x)/2; 25 return cnt <= 0; 26 } 27 int main() 28 { 29 T = read(); 30 while (T--) 31 { 32 n = read(), ans = -1, mx = 0, mn = 2e9; 33 for (int i=1; i<=n; i++) a[i] = read(), mx = std::max(mx, a[i]), mn = std::min(mn, a[i]); 34 l = mn, r = mx; 35 for (int mid=(l+r)>>1; l<=r; mid=(l+r)>>1) 36 if (check(mid)) ans = mid, l = mid+1; 37 else r = mid-1; 38 printf("%d\n",ans); 39 } 40 return 0; 41 }
END