标签:
二分中值,检查的时候继续二分,二分套二分。。。。。
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<functional> #include<queue> using namespace std; typedef long long ll; const int maxv=1e5+30; ll n; ll tol; ll a[maxv]; bool check(ll mid){ ll num=0; for(int i=0;i<n;i++){ if(a[i]+mid>a[n-1]) break; ll *p=lower_bound(a+i,a+n,a[i]+mid); num+=n-(p-a); } if(num>(tol/2)) return 1; else return 0; } int main(){ freopen("in","r",stdin); while(cin>>n){ tol=n*(n-1)/2; for(int i=0;i<n;i++) scanf("%lld",&a[i]); sort(a,a+n); ll l=0,r=1e9; while(r-l>1){ ll mid=(r+l)/2; if(check(mid)) l=mid; else r=mid; } if(check(l)) cout<<l<<endl; else cout<<r<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/Cw-trip/p/4467719.html