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 ...
随机推荐
- (一)使用twisted Deferred
一.开篇 为什么是twisted,twisted作为一个python网络编程框架,出道早,但一直不温不火,这几年和tornado比起来,更是近乎销声匿迹:但作为初学者,觉得twisted还是有很多优点 ...
- 什么是云数据库POLARDB
POLARDB是阿里巴巴自主研发的下一代关系型分布式云原生数据库,目前兼容三种数据库引擎:MySQL.Oracle.PostgreSQL.计算能力最高可扩展至1000核以上,存储容量最高可达 100T ...
- 从入门到自闭之Python编码
ascii码: 支持英文,数字,符号 1字节 不支持中文 gbk(国标) 支持英文,数字,符号 1字节 支持中文 2字节 unicode(万国码): 支持英文,数字,符号 4字节 支持欧洲 4字节 支 ...
- 搭建springCloud网关zuul
一.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- 服务器部署Java Web及微信开发调试
参考摘抄: 阿里云部署Java网站和微信开发调试心得技巧(上):https://www.imooc.com/article/20583 阿里云部署Java网站和微信开发调试心得技巧(下):https: ...
- Shell随机生成字符串
随机生成18位的字符串,数字 大小写字符 斜线 password=`openssl rand -base64 |-`
- 主流浏览器内核(IE、Chrome、Firefox、Safari、Opera)
IE浏览器,使用Trident浏览器内核,又称为IE内核.只用于Windows平台,而且并不是开源的: chrome浏览器,目前使用的是Blink浏览器内核.浏览器内核的演进过程:Chromium ...
- jquery 滚动事件-记录自己常用的
1.h5端页面滑动至第3屏及以后才出现置顶按钮 $(document).scroll(function() { var scroH = $(document).scrollTop(); //滚动高度 ...
- js 回顾知识总结一
1.js数据类型? 基本数据类型:String(字符串).boolean(布尔值).Number(数字).undefined(未定义).null(空) 引用数据类型:Object(对象).Array( ...
- 1.(基础)tornado初识
tornado的话就不带着大家看源码了,今后可能会介绍,目前只是看简单的用法,而且当前的tornado版本不高,其实说白了这是很久以前写的文档,但是由于格式的原因,所以打算用Markdown重写一次. ...