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) ...
随机推荐
- 049、Java中使用switch判断,不加入break时的操作
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- setTimeout的异步
http://www.cnblogs.com/littledu/articles/2607211.html http://www.cnblogs.com/rubylouvre/archive/2009 ...
- leetcode1162 As Far from Land as Possible
""" Given an N x N grid containing only values and , represents water and represents ...
- url中?的作用
http://123.206.87.240:8002/get/?what=flag? 分隔实际的URL和参数 ,用于动态页面的交互和传参
- tomcat启动报错The JRE could not be found.Edit the server and change the JRE location
解决: 在Windows->Preferences->Server->Runtime Environments 选择Tomcat->Edit,在jre中选择相应的jdk版本,完 ...
- Oracle 查询当前用户下的所有表
select table_name from user_tables;
- 设置gvim的字体大小
1.临时设置: 进入命令行模式输入: set guifont=Courier\ New:h10 2.永久设置: 打开安装目录找到defaults.vim在最后一行输入: set guifont=Cou ...
- [LeetCode] 928. Minimize Malware Spread II 最大程度上减少恶意软件的传播之二
(This problem is the same as Minimize Malware Spread, with the differences bolded.) In a network of ...
- java中JDBC当中请给出一个DataSource的HelloWorld例子
在前面 的jdbc的Helloworld程序当中,我们用DriverManager来获取数据库连接.事实上通过这种方法获取数据库连接,是比较耗费计算机资 源的.当然了,这也是没有办法的事儿.就像我们买 ...
- 118-PHP调用带参数的成员方法
<?php class ren{ //定义人类 public function info($name,$age=3){ //定义有两个参数的成员方法 echo "我是{$name},年 ...