zoukankan      html  css  js  c++  java
  • cf702C Cellular Network

    C. Cellular Network
    time limit per test 3 seconds
    memory limit per test 256 megabytes
    input standard input
    output standard output

    You are given n points on the straight line — the positions (x-coordinates) of the cities and m points on the same line — the positions (x-coordinates) of the cellular towers. All towers work in the same way — they provide cellular network for all cities, which are located at the distance which is no more than r from this tower.

    Your task is to find minimal r that each city has been provided by cellular network, i.e. for each city there is at least one cellular tower at the distance which is no more than r.

    If r = 0 then a tower provides cellular network only for the point where it is located. One tower can provide cellular network for any number of cities, but all these cities must be at the distance which is no more than r from this tower.

    Input

    The first line contains two positive integers n and m (1 ≤ n, m ≤ 105) — the number of cities and the number of cellular towers.

    The second line contains a sequence of n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the coordinates of cities. It is allowed that there are any number of cities in the same point. All coordinates ai are given in non-decreasing order.

    The third line contains a sequence of m integers b1, b2, ..., bm ( - 109 ≤ bj ≤ 109) — the coordinates of cellular towers. It is allowed that there are any number of towers in the same point. All coordinates bj are given in non-decreasing order.

    Output

    Print minimal r so that each city will be covered by cellular network.

    Examples
    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],求出min{ |a[i]-b[j]| }

    然后再在n个数中取个最大值输出

    简直就是模拟嘛

    这里求min我又用了二分(应该三分也可以,吧。毕竟有个abs就是单峰的)

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<ctime>
     8 #define LL long long
     9 #define inf 0x7ffffff
    10 #define pa pair<int,int>
    11 #define pi 3.1415926535897932384626433832795028841971
    12 using namespace std;
    13 inline LL read()
    14 {
    15     LL x=0,f=1;char ch=getchar();
    16     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    17     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    18     return x*f;
    19 }
    20 inline void write(LL a)
    21 {
    22     if (a<0){printf("-");a=-a;}
    23     if (a>=10)write(a/10);
    24     putchar(a%10+'0');
    25 }
    26 inline void writeln(LL a){write(a);printf("
    ");}
    27 int n,m,ans;
    28 int a[100010];
    29 int b[100010];
    30 int dist[100010];
    31 inline int bsearch(int l,int r,int dat)
    32 {
    33     int ans=0;
    34     while (l<=r)
    35     {
    36         int mid=(l+r)>>1;
    37         if (b[mid]<dat){ans=mid;l=mid+1;}
    38         else r=mid-1;
    39     }
    40     return ans;
    41 }
    42 int main()
    43 {
    44     n=read();m=read();
    45     for(int i=1;i<=n;i++)a[i]=read();
    46     sort(a+1,a+n+1);
    47     for (int i=1;i<=m;i++)b[i]=read();
    48     sort(b+1,b+m+1);
    49     int now=1;
    50     for (int i=1;i<=n;i++)
    51     {
    52         int fnd=bsearch(1,m,a[i]);
    53         if (fnd==0)fnd++;dist[i]=abs(b[fnd]-a[i]);
    54         if (fnd!=m)dist[i]=min(abs(b[fnd]-a[i]),abs(b[fnd+1]-a[i]));
    55         ans=max(ans,dist[i]);
    56     }
    57     printf("%d
    ",ans);
    58 }
    cf702D
    ——by zhber,转载请注明来源
  • 相关阅读:
    CSS清除浮动常用方法小结
    json格式的javascript对象用法分析
    jQuery on()方法绑定动态元素的点击事件无响应的解决办法
    一道思考题(二进制枚举的应用的想法)切金条
    对于excel中,无法把字符型转成数字型解决办法
    对于excel中,无法把字符型转成数字型解决办法
    python文本挖掘输出权重,词频等信息,画出3d权重图
    python文本挖掘输出权重,词频等信息,画出3d权重图
    python使用scikit-learn计算TF-IDF
    python使用scikit-learn计算TF-IDF
  • 原文地址:https://www.cnblogs.com/zhber/p/5742922.html
Copyright © 2011-2022 走看看