CodeForces 51C 二分搜索
校队选拔神马的事情就不说了,哥们反正是要崛起的人了!
感谢何骐的提醒。
校队选拔的时候又被二分给坑了,所以还想做几道二分搜索的题目来练练手。
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
The New Vasjuki village is stretched along the motorway and that's why every house on it is characterized by its shift relative to some fixed point — the xi coordinate. The village consists of n houses, the i-th house is located in the point with coordinates of xi.
TELE3, a cellular communication provider planned to locate three base stations so as to provide every house in the village with cellular communication. The base station having power d located in the point t provides with communication all the houses on the segment[t - d, t + d] (including boundaries).
To simplify the integration (and simply not to mix anything up) all the three stations are planned to possess the equal power of d. Which minimal value of d is enough to provide all the houses in the village with cellular communication.
Input
The first line contains an integer n (1 ≤ n ≤ 2· 105) which represents the number of houses in the village. The second line contains the coordinates of houses — the sequence x1, x2, ..., xn of integer numbers (1 ≤ xi ≤ 109). It is possible that two or more houses are located on one point. The coordinates are given in a arbitrary order.
Output
Print the required minimal power d. In the second line print three numbers — the possible coordinates of the base stations' location. Print the coordinates with 6 digits after the decimal point. The positions of the stations can be any from 0 to 2· 109 inclusively. It is accepted for the base stations to have matching coordinates. If there are many solutions, print any of them.
Sample Input
4
1 2 3 4
0.500000
1.500000 2.500000 3.500000
3
10 20 30
0
10.000000 20.000000 30.000000
5
10003 10004 10001 10002 1
0.500000
1.000000 10001.500000 10003.500000
初看这个题目的时候,我以为又是切蛋糕那种题目,二分一个浮点数,(事实上根据网上博客贴的代码,有人这样做也可以过)。但是后来接触到一种思路,事实上点坐标都是整数,所以站点的覆盖范围,即覆盖圆的直径,必定是一个整数,故,只要二分覆盖圆的直径,即可。
关于最后还要输出每个站点的具体坐标,一个好的方法是在二分时用个数组记录每建立一个覆盖点后,覆盖到的点的下一点。这样,求坐标的时候,还是在代码里面说吧
比较坑的是第一组数据,我为这个纠结了好久,明显的在第一组数据中,两个站点就可以覆盖4个点,第三个站点随便怎么放,但我以为一定要按样例,改来改去,都没法统一
结果发现根本就不用管这个破第一组样例,也过了。可能像这种数据,随便站点怎么放,只要能覆盖就过了吧。
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 200000
#include <algorithm>
using namespace std;
int loc[maxn+];
int n;
int ans[];
int fnext(int y) //用来找到当前覆盖圆覆盖后的最近一个覆盖点。
{
int l=,r=n+,mid=(l+r)/;
while (l<r)
{ if (loc[mid]<=y)
l=mid+;
else
r=mid;
mid=(l+r)/;
}
return mid;
}
int ok(int x)
{
int i,j;
int z=;
for (i=; i<=; i++)
{ z=fnext(loc[z]+x);
ans[i]=z; //用这个数组来记录覆盖圆的下一个点。
//cout<<x<<" "<<z<<endl;
if (z>=n+) return ;
} return ;
}
int main()
{ while (scanf("%d",&n)!=EOF)
{
int i,j,k;
for (i=; i<=n; i++)
{
scanf("%d",&loc[i]);
//cout<<loc[i]<<endl;
}
sort(loc+,loc+n+);
//loc[n+1]=loc[n];
int r=(loc[n]-loc[]);
int l=,mid;
while (l<r) //二分覆盖圆的直径
{
mid=(r+l)/;
if (ok(mid)) r=mid;
else l=mid+;
}
ok(l);
//cout<<ans[1]<<" "<<ans[2]<<" "<<ans[3]<<endl;
double radius=l*1.0/2.0;
double radar1=(loc[ans[]-]+loc[])*1.0/2.0;
double radar2=(loc[ans[]-]+loc[ans[]])/2.0;//前点加后点再除以2的方式得到圆心坐标
double radar3=(loc[ans[]-]+loc[ans[]])/2.0;
printf("%.6f\n",radius);
printf("%.6f ",radar1);
printf("%.6f ",radar2);
printf("%.6f\n",radar3);
}
return ;
}
CodeForces 51C 二分搜索的更多相关文章
- 二分搜索 Codeforces Round #299 (Div. 2) C. Tavas and Karafs
题目传送门 /* 题意:给定一个数列,求最大的r使得[l,r]的数字能在t次全变为0,每一次可以在m的长度内减1 二分搜索:搜索r,求出sum <= t * m的最大的r 详细解释:http:/ ...
- 二分搜索 Codeforces Round #218 (Div. 2) C. Hamburgers
题目传送门 /* 题意:一个汉堡制作由字符串得出,自己有一些原材料,还有钱可以去商店购买原材料,问最多能做几个汉堡 二分:二分汉堡个数,判断此时所花费的钱是否在规定以内 */ #include < ...
- Codeforces Round #448 (Div. 2) B. XK Segments【二分搜索/排序/查找合法的数在哪些不同区间的区间数目】
B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- codeforces 1019B The hat 【交互题+二分搜索】
题目链接:戳这里 学习题解:戳这里
- codeforces 483B Friends and Presents 解题报告
题目链接:http://codeforces.com/problemset/problem/483/B 题目意思:有两个 friends,需要将 cnt1 个不能整除 x 的数分给第一个friend, ...
- CodeForces - 363D --二分和贪心
题目:CodeForces - 363D 题意:给定n个学生,其中每个学生都有各自的私己钱,并且自己的私己钱只能用在自己买自行车,不能给别人. 给定m个自行车,每个自行车都有一个价格. 给定公有财产a ...
- CodeForces 551C - GukiZ hates Boxes - [二分+贪心]
题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...
- Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】
传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...
- Codeforces Round #404 (Div. 2) C 二分查找
Codeforces Round #404 (Div. 2) 题意:对于 n and m (1 ≤ n, m ≤ 10^18) 找到 1) [n<= m] cout<<n; 2) ...
随机推荐
- 十八、React react-router4.x中:实现路由模块化、以及嵌套路由父子组件传值
一.路由模块化(用字典定义路由,然后循环出来) 1.官方文档参考 [官方文档]https://reacttraining.com/react-router/web/guides/quick-start ...
- window下Apache Jmeter基础用法
1.下载Apache-jmeter-5.1.1.zip 2.解压到一个地方,就可以开始使用了,如果需要在CMD里快速打开,可以设置环境变量. 3.在bin里面,直接打开jmeter.bat. 可以修改 ...
- 用fiddler监控移动端的通讯
用fiddler监控移动端的通讯 1 依次打开Fiddler->Tools->Fiddler Options在[Connection]面板里将Allow remote computers ...
- 中文文本分类之TextRNN
RNN模型由于具有短期记忆功能,因此天然就比较适合处理自然语言等序列问题,尤其是引入门控机制后,能够解决长期依赖问题,捕获输入样本之间的长距离联系.本文的模型是堆叠两层的LSTM和GRU模型,模型的结 ...
- Python中的numpy函数的使用ones,zeros,eye
在看别人写的代码时,看到的不知道的函数,就在这里记下来. 原文是这样用的: weights = ones((numfeatures,1)) 在python中help(): import numpy a ...
- 049-PHP输出当前文件的名称
<?php echo __FILE__; //利用常量__FILE__输出当前文件的名称 ?>
- tcp协议与dup协议知识总结
在工作之余用xmind总结了一些UDP协议与TCP协议的知识点,如果有需要可以通过下方的留言,分享xmind文件和xmind软件.
- 启用sql日志
SHOW VARIABLES LIKE "general_log%"; -- 查询是否启用日志 SET GLOBAL general_log = 'ON'; -- 设置启用 SE ...
- UVA - 10689 Yet another Number Sequence (矩阵快速幂求斐波那契)
题意:已知f(0) = a,f(1) = b,f(n) = f(n − 1) + f(n − 2), n > 1,求f(n)的后m位数. 分析:n最大为109,矩阵快速幂求解,复杂度log2(1 ...
- 【redis】redis底层数据结构原理--简单动态字符串 链表 字典 跳跃表 整数集合 压缩列表等
redis有五种数据类型string.list.hash.set.zset(字符串.哈希.列表.集合.有序集合)并且自实现了简单动态字符串.双端链表.字典.压缩列表.整数集合.跳跃表等数据结构.red ...