src:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=826&pid=1004
可以用某值来判断是否满足题意时 可以用二分答案的方法!
代码:
#include<bits/stdc++.h> using namespace std; #define per(i,a,b) for(int i=a;i <= b;i++) #define Max(a,b) a=max(a,b) #define Min(a,b) a=min(a,b) #define Sz(x) (int)x.size() typedef long long ll; ll gcd(ll a,ll b){while(b){ll t=b;b=a%b;a=t;}return a;} const int inf=0x3f3f3f3f; const int mod=1000000007; #define siz 300005 int n,num[siz]; bool check(int x) { ll a=0,b=0; per(i,1,n){ if(num[i]>x){a+=(num[i]-x)/2;} else b+=x-num[i]; } return a>=b; } int main() { int T; scanf("%d",&T); while(T--){ scanf("%d",&n); per(i,1,n)scanf("%d",&num[i]); int l=0,r=1e8+2,mid,ans=0; while(l<=r){ mid=(l+r)>>1; if(check(mid)==true){ans=mid;l=mid+1;} else r=mid-1; } printf("%d\n",ans); } return 0; }
原文地址:https://www.cnblogs.com/WindFreedom/p/9465372.html
时间: 2024-10-08 21:38:44