標籤:net str logs 尋找 最小 http long names put
Cellular Network
CodeForces - 702C
給定 n (城市數量) 和 m (燈塔數量);
給定 a1~an 城市座標;
給定 b1~bm 燈塔座標;
求出燈塔照亮的最小半徑 r ,使得所有城市都能被照亮。
Input
3 2
-2 2 4
-3 0
Output
4
Input
5 3
1 5 10 14 17
4 11 15
Output
3
題解:
首先對於每個城市 a[ i ],找到離它最近的左右兩個燈塔 b [ x ] , b [ x-1 ](只有最左或最右燈塔時 特判),然後比較這兩個燈塔與該城市的距離,取最小值 F [ i ] 。
對於所有的城市,要求出燈塔最小半徑,要在 F [ 1 ] ~ F [ n ] 中求最大值。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>using namespace std;typedef long long ll;const int N=1e5+50;ll a[N];ll b[N];int n,m;int main(){ scanf("%d%d",&n,&m); for(int i=0;i<n;i++) scanf("%lld",&a[i]); for(int i=0;i<m;i++) scanf("%lld",&b[i]); ll ans=0; for(int i=0;i<n;i++) { int x=lower_bound(b,b+m,a[i])-b; ll temp; if(x==0)//只有最左邊燈塔 temp=abs(a[i]-b[0]); else if(x==m)//只有最右邊燈塔 temp=abs(a[i]-b[m-1]); else //左右燈塔取最近之一 temp=min(abs(a[i]-b[x]),abs(a[i]-b[x-1])); ans=max(ans,temp);//在各個城市與燈塔的距離中保留最大值比較出 r } cout<<ans<<endl; return 0;}
CodeForce-702C Cellular Network(尋找)