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) ...
随机推荐
- DCGAN生成目标训练图片
前言: GAN的原理很简单,但是它有很多变体,如:DCGAN.CycleGAN.DeblurGAN等,它们也被用在不同地方,本文将用到DCGAN来生成头像图片,可以做到以假乱真的地步. 1.首先调用程 ...
- 使用Python绘制漫步图
代码如下: import matplotlib.pyplot as plt from random import choice class RandomWalk(): def __init__(sel ...
- LeetCode160 相交链表(双指针)
题目: click here!!题目传送门 思路: 1.笨方法 因为如果两个链表相交的话,从相交的地方往后是同一条链表,所以: 分别遍历两个链表,得出两个链表的长度,两个长度做差得到n,然后将长的链表 ...
- CAN通讯基本设置
A节点pelican协议下,扩展帧 单滤波方式 A节点的接收滤波器 ID号 设置为 0x19881205 设置过程 (1) pelican模式设置 设置时钟分频寄存器CDR.7 =1 使SJA1000 ...
- NAND厂商哭晕:减产也阻止不了跌价
导读 NAND闪存价格已经连跌了6个季度,这让上游NAND厂商三星.东芝.美光等损失惨重,纷纷削减NAND产能.在群联台北电脑展上,群联公司董事长潘建成也预测NAND闪存价格已经跌破了成本,未来跌幅会 ...
- django 中从外界借助多个网站时 static 的存放和整理
在 模板之家中 前端页面直接上去抓取 可是遇到重复 或者 版本不统一 所以 在每个app下面建立自己的 stastic 在制作的html 页面上方 导入静态页面 {% load static ...
- css选择器权重、样式继承、默认样式
学过css的小伙伴都是指css选择器的权重 !important Infinity 行间样式 1000 id 100 class|属性|伪类 10 标签|伪元素 1 通配符 0 权重相同 相同cs ...
- 【LeetCode】160. 相交链表
题目 输入两个链表,找出它们的第一个公共节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...
- HDU - 4576 Robot(概率dp+滚动数组)
题意:所有的格子围成一个圈,标号为1~n,若从格子1出发,每次指令告知行走的步数,但可能逆时针也可能顺时针走,概率都是1/2,那么问走了m次指令后位于格子l~r(1≤l≤r≤n)的概率. 分析: 1. ...
- UVA - 1612 Guess (猜名次)(贪心)
题意:有n(n<=16384)位选手参加编程比赛.比赛有3道题目,每个选手的每道题目都有一个评测之前的预得分(这个分数和选手提交程序的时间相关,提交得越早,预得分越大).接下来是系统测试.如果某 ...