Educational Codeforces Round 15 C. Cellular Network (二分)
来源:互联网 发布:追韩国女生知乎 编辑:程序博客网 时间:2024/05/22 00:40
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.
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.
Print minimal r so that each city will be covered by cellular network.
3 2-2 2 4-3 0
4
5 31 5 10 14 174 11 15
3
题意:有n座城市,和m座信号塔,要你找出这些信号塔能覆盖所有城市的最短距离。
题解: 对信号塔b进行二分,找城市与信号塔的最小距离即可。(题目给的数据是严格非连续递增的)
AC代码:
#include<bits/stdc++.h>#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<cstring>#include<vector>#include<map>#include<queue>#include<set>#include<stack>using namespace std;typedef long long ll;typedef unsigned long long ull;#define MP(x,y) make_pair(x,y) template <class T1, class T2>inline void getmax(T1 &a, T2 b) { if (b>a)a = b; } template <class T1, class T2>inline void getmin(T1 &a, T2 b) { if (b<a)a = b; }int read(){int v = 0, f = 1;char c =getchar();while( c < 48 || 57 < c ){if(c=='-') f = -1;c = getchar();}while(48 <= c && c <= 57) v = v*10+c-48, c = getchar();return v*f;}int main(){//freopen("in.txt","r",stdin);int n,m;cin>>n>>m;int a[n+1],b[m+1];for(int i = 0; i < n; i++){cin>>a[i];}for (int i = 0; i < m; i++){cin>>b[i];}int ans = 0 ;for(int i = 0; i<n; i++){int l = 0, r = m-1; //对信号塔b进行二分,找城市与信号塔的最小距离while(r-l > 1){int mid = (r+l)>>1;if(b[mid] >= a[i]){r = mid;}else{l=mid;}}int cnt = min(abs(a[i]-b[l]),abs(a[i]-b[r]));ans = max(ans, cnt);}cout<<ans;return 0;}
- Educational Codeforces Round 15 C. Cellular Network(二分)
- Educational Codeforces Round 15 C. Cellular Network (二分)
- Educational Codeforces Round 15 C Cellular Network(二分)
- Educational Codeforces Round 15 C. Cellular Network
- Educational Codeforces Round 15, problem: (C) Cellular Network
- 【CodeForces】702C - Cellular Network(二分)
- 【Codeforces】-702C-Cellular Network(二分)
- codeforces 702C Cellular Network(二分+尺取)
- codeforces 702C Cellular Network 二分
- Educational Codeforces Round 15 C 二分+差分数组
- Educational Codeforces Round 17 C. Two strings(二分)
- Educational Codeforces Round 17 C 二分
- Cf Edu 15 C Cellular Network 二分
- coderforces 702C Cellular Network(二分)
- CF Educational Codeforces Round 15(C)
- CodeForces 702 C. Cellular Network(水~)
- Educational Codeforces Round 21 D ( 二分 )
- codeforces-702C-Cellular Network
- 复制文件或目录到另一个目录下
- C++多态的实现原理
- part2:数据访问和dom操作的优化
- 非锐化掩蔽(Unsharp Masking)与高提升滤波
- |Tyvj|NOIP2004|堆|贪心|P1066 合并果子
- Educational Codeforces Round 15 C. Cellular Network (二分)
- JAVA GC回收方式
- printf输出的一些形式
- spring、hibernate的整合
- PHP普通缩放和等比缩放函数
- SVNKit模式下svn working copy locked的问题
- RSA 分段加解密-解决“不正确的长度”的数据
- Java之implements
- OJ------记票统计