CodeForce-702C Cellular Network(尋找)

來源:互聯網
上載者:User

標籤: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(尋找)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.