题目链接:C. Cellular Network
让每一个城市都可以被供给,也就是说每一个发电站的距离向左右延伸后要把城市最小~最大范围全部覆盖到,想了一会儿感觉是找每一个城市的最近供给站的距离,然后取最大值作为标准,然后进行二分……,然后怎么找这个最近的供给站呢?还是二分……,找的时候注意边界就可以了
代码:
#include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f #define MM(x,y) memset(x,y,sizeof(x)) #define LC(x) (x<<1) #define RC(x) ((x<<1)+1) #define MID(x,y) ((x+y)>>1) typedef pair<int,int> pii; typedef long long LL; const double PI=acos(-1.0); const int N=100010; int n,m; int c[N]; int d[N]; int vis[N]; LL min_dis[N]; inline LL ABS(const LL &a) { return a>0?a:-a; } int main(void) { int i,j,k; while (~scanf("%d%d",&n,&m)) { for (i=0; i<n; ++i) { scanf("%d",&c[i]); } for (i=0; i<m; ++i) { scanf("%d",&d[i]); } LL dx=0; for (i=0; i<n; ++i) { int l=lower_bound(d,d+m,c[i])-d; int r=upper_bound(d,d+m,c[i])-d-1; if(l>=m) l=m-1; if(r>=m) r=m-1; if(r<0) r=0; min_dis[i]=min(ABS((LL)c[i]-d[l]),ABS((LL)c[i]-d[r])); if(min_dis[i]>dx) dx=min_dis[i]; } int nm=n+m; LL l=0,r=2LL*1e9,mid,ans; while (l<=r) { mid=(l+r)>>1; if(mid>=dx) { r=mid-1; ans=mid; } else l=mid+1; } printf("%I64d ",ans); } return 0; }