poj 1064 高精度 二分
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 32191 | Accepted: 6888 |
Description
using a "star" topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it.
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants
as far from each other as possible.
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not
known and the Cable Master is completely puzzled.
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.
Input
by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two
digits after a decimal point.
Output
two digits after a decimal point.
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number "0.00" (without quotes).
Sample Input
4 11
8.02
7.43
4.57
5.39
Sample Output
2.00
Source
解法一:浮点数二分 直接在double上二分
错因:没有向下取整
解答:二分+高精度
<span style="color:#3333ff;">#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
double a[10005];
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
for(int i=1;i<=n;i++)
scanf("%lf",&a[i]);
int t=100;double r=100000,l=0,mid;
while(t--)
{
int cnt=0;mid=(r+l)/2.0;
for(int i=1;i<=n;i++)
cnt+=int(a[i]/mid); //记得取整
if(cnt>=k)
l=mid; //尽量向大的取
else
r=mid;
}
</span><span style="color:#ff0000;">printf("%0.2f\n",(floor(r*100))/100); //注意利用floor函数向下取整,所以要预先*100!!!!!!!!!!向下取整是关键</span><span style="color:#3333ff;">
}
return 0;
}</span>
解法二:整数二分 先乘以100再运算,最后/100
错因:l需要初始化为1而不能是0,否则会re;!!!!!!!!!!!!
解答:二分
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[10005];
int main()
{
int n,k,maxn;double temp;
while(~scanf("%d %d",&n,&k))
{
maxn=0;
for(int i=1;i<=n;i++)
{
scanf("%lf",&temp);
a[i]=int(temp*100);
if(a[i]>maxn)
maxn=a[i];
}
int mid,r=maxn,l=1;//l初始化为0会re,,因为l==0时若r==1或0则mid==0,下面的除以mid就会re!!!!!!!!!
while(l<=r)
{
int cnt=0;mid=(r+l)/2;
for(int i=1;i<=n;i++)
cnt+=int(a[i]/mid);
if(cnt>=k)
l=mid+1;
else
r=mid-1;
}
printf("%0.2lf\n",r*0.01);//注意是*0.01,不能为/100,因为r是整型
}
return 0;
}
poj 1064 高精度 二分的更多相关文章
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- poj 1064 Cable master 判断一个解是否可行 浮点数二分
poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...
- POJ 2109 Power of Cryptography【高精度+二分 Or double水过~~】
题目链接: http://poj.org/problem?id=2109 参考: http://blog.csdn.net/code_pang/article/details/8263971 题意: ...
- poj 1064 Cable master 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...
- POJ 1064 1759 3484 3061 (二分搜索)
POJ 1064 题意 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留小数点后2位. 思路 二分搜索.这里要注意精度问题,代码中有详细说 ...
- poj 2318 叉积+二分
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13262 Accepted: 6412 Description ...
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- 二分搜索 POJ 1064 Cable master
题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...
- POJ 1064 (二分)
题目链接: http://poj.org/problem?id=1064 题目大意:一堆棍子可以截取,问要求最后给出K根等长棍子,求每根棍子的最大长度.保留2位小数.如果小于0.01,则输出0.00 ...
随机推荐
- [转帖]linux下使用 du查看某个文件或目录占用磁盘空间的大小
linux下使用 du查看某个文件或目录占用磁盘空间的大小 du -ah --max-depth= 去年用过一次 后来忘记了.. 命令这个东西 熟能生巧.. https://www.cnblogs.c ...
- 设计模式:建造者模式(Builder)
流水作业大家应该都清楚吧!在流水作业中,我们可以将一些复杂的东西给构建出来,例如汽车.我们都知道汽车内部构件比较复杂,由很多部件组成,例如车轮.车门.发动机.方向盘等等,对于我们用户来说我们并不需要知 ...
- Go语言中的切片(十)
go中数组的长度是固定的,且不同长度的数组是不同类型,这样的限制带来不少局限性.于是切片就来了,切片(Slice)是一个拥有相同类型元素的可变长度的序列.它是基于数组类型做的一层封装.它非常灵活,支持 ...
- Python_4day
函数 函数可以用来定义可重复代码,组织和简化 一般来说一个函数在实际开发中为一个小功能 一个类为一个大功能 同样函数的长度不要超过一屏 Python中的所有函数实际上都是有返回值(return N ...
- JS基础_标识符
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 一文搞懂网络知识,IP、子网掩码、网关、DNS、端口号
网络的基本概念 客户端:应用 C/S(客户端/服务器) B/S(浏览器/服务器) 服务器:为客户端提供服务.数据.资源的机器 请求:客户端向服务器索取数据 响应:服务器对客户端请求作出反应,一般是返回 ...
- ES6 新增的数组的方法
给定一个数组 let list = [ // wu: 武力 zhi:智力 { id: 1, name: '张飞', wu: 97, zhi: 10 }, { id: 2, name: '诸葛亮', w ...
- 解决postgresql数据库localhost可以连接,ip连接不了的问题
解决:windows环境下,postgresql数据库,localhost可以连接,ip地址连接不了. 解决办法: 1.打开postgresql安装目录下的配置文件 pg_hba.conf ...
- Object.keys()返回对象自身可枚举属性组成的数组
Object.keys()方法是对一个对象的key遍历,会把key组成一个数组返回 示例: // 参数为数组时,返回的是数组的索引 let arr1 = [1, 2, '3'] console.log ...
- 互联网安全架构之常见的Web攻击手段及解决办法
一.Web 安全常见攻击手段 XSS(跨站脚本攻击) SQL 注入 CSRF(跨站请求伪造) 上传漏洞 DDoS(分布式拒绝服务攻击)等 二.攻击手段原理及解决方案 1.XSS攻击 原理:XSS 攻击 ...