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 ...
随机推荐
- Python 过滤HTML实体符号简易方法
html_tag = {' ': '\n', '"': '\"', '&': '', '<': '<', '>': '>', '' ...
- Idea中新建maven项目的目录结构
maven项目的目录结构如下所示 pom.xml文件内容如下所示 <?xml version="1.0" encoding="UTF-8"?> &l ...
- FFmpeg4.0笔记:本地媒体文件解码、帧格式转换、重采样、编码、封装、转封装、avio、硬解码等例子
Github https://github.com/gongluck/FFmpeg4.0-study/blob/master/official%20example/my_example.cpp #in ...
- Mac 安装 Homebrew
为什么要在 MAC 上安装 Homebrew 它干什么用的呢?我们知道在 CentOS 和 Ubuntu 上都有自己的包管理工具,但是在 MAC 上却没有这样类似的管理工具. # CentOS $ y ...
- mysql事务、redo日志、undo日志、checkpoint详解
转载: https://zhuanlan.zhihu.com/p/34650908 事务: 说起mysql innodb存储引擎的事务,首先想到就是ACID(不知道的请google),数据库是如何做到 ...
- 在Linux中,没有文件创建时间的概念。只有文件的访问时间、修改时间、状态改变时间
在Linux中,没有文件创建时间的概念.只有文件的访问时间.修改时间.状态改变时间.也就是说不能知道文件的创建时间.但如果文件创建后就没有修改过,修改时间=创建时间:如果文件创建后,状态就没有改变过, ...
- document.body.scrollTop无效的解决方法
1.document.body.scrollTop = 0 有时候不生效,两种解决方案,试试看. 1-1.设置:document.documentElement.scrollTop = 0;1-2.设 ...
- Js实现图片点击切换与轮播
Js实现图片点击切换与轮播 图片点击切换 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...
- 精确率与回召率与 F1-Meature
例子: true positive(真正例): 把 Colin power预测为Colin power(55) false positive(假正例): 把 其他人预测为Colin power(4+1 ...
- 问题:com.alibaba.dubbo.rpc.RpcException: Failed to invoke ......
个人解决流程: 一看到这个问题,下意识想到了是dubbo远程连接的问题,可能是dubbo本身的问题,于是在虚拟机上另外一台dubbo能正常脸上的服务器上重新尝试,还是报相同的错误,并且在dubbo-a ...