poj1064 二分,注意精度!
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 35269 | Accepted: 7513 |
Description
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
Output
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
题目大意:有n根电缆,你需要k条等同长度的电缆,最后得到的电缆长度最长是多少。
思路分析:初学二分,做这道题的时候感觉和之前做的一道题非常相似,以为可以轻松切掉,可是在做的时候还是出现了问题,
正确的思路应该是化为厘米,然后用整数二分,如果直接用小数二分最后会出现问题四舍五入,对于这些数据
4 2540
8.02
7.43
4.57
5.39 =>0.01 4 2542
8.02
7.43
4.57
5.39 =>0.00
化为整数进行处理则可以避免这些问题,另外要注意二分上下限,下限自然是0,而上限你可以用sum/k,也可以用a[n-1],当出现sum的时候,
就会超过int数据范围,要用__int64,如果用a[n-1]为上界就不需要开__int64了,再就是写函数时要判断是否有死循环,我就写错了,狂
TLE不止orz.
代码:
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<iostream>
#include<algorithm>
#include <cmath>
#define LL long long
using namespace std;
const int maxn=10000+100;
const double pi=acos(-1.0);
double a[maxn];
int b[maxn];
int n,k;
__int64 sum;
bool check(int x)
{
int t=0;
for(int i=0;i<n;i++)
{
int m=b[i];
while(m>=x)
{
m-=x;
t++;
if(t>=k) return true;
}
}
return false;
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
sum=0;
for(int i=0;i<n;i++)
{
scanf("%lf",&a[i]);
b[i]=a[i]*100;//将单位化为整数厘米
sum+=b[i];
}
sort(b,b+n);
int l=0,r=b[n-1];
int ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%.2lf\n",ans*0.01);
}
return 0;
}
poj1064 二分,注意精度!的更多相关文章
- HDU 1007 Quoit Design(二分+浮点数精度控制)
Quoit Design Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- UVA 10341 Solve It 解方程 二分查找+精度
题意:给出一个式子以及里面的常量,求出范围为[0,1]的解,精度要求为小数点后4为. 二分暴力查找即可. e^(-n)可以用math.h里面的exp(-n)表示. 代码:(uva该题我老是出现Subm ...
- Cable master---poj1064(二分|卡精度)
题目链接:http://poj.org/problem?id=1064 题意:有n条绳子,长度为Li,现在从这n条绳子中切割出K条相等的绳子,问切割出来的这k条绳子的最大长度为多少: 二分判断即可: ...
- POJ3111 K Best(另类背包+二分+变态精度)
POJ3111 K Best,看讨论区说数据有点变态,精度要求较高,我就直接把循环写成了100次,6100ms过,(试了一下30,40都会wa,50是4000ms) 第一次在POJ上看到下面这种东西还 ...
- 【二分贪心+精度问题】F. Pie
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/F [题意] 给定n个已知半径的披萨,有m个人要分这n个披萨 要求每个人分到的面积 ...
- hdu.. 基础二分的精度问题
#include<stdio.h>#include<iostream>using namespace std;double f(double x){ return 8*x*x* ...
- poj1064 Cable master(二分查找,精度)
https://vjudge.net/problem/POJ-1064 二分就相当于不停地折半试. C++AC,G++WA不知为何,有人说C函数ans那里爆int了,改了之后也没什么用. #inclu ...
- poj 3525 半平面交求多边形内切圆最大半径【半平面交】+【二分】
<题目链接> 题目大意:给出一个四面环海的凸多边形岛屿,求出这个岛屿中的点到海的最远距离. 解题分析: 仔细思考就会发现,其实题目其实就是让我们求该凸多边形内内切圆的最大半径是多少.但是, ...
随机推荐
- sprintf()详细介绍
sprintf 编辑词条 编辑词条 --> 字串格式化命令,主要功能是把格式化的数据写入某个字符串中.sprintf 是个变参函数,使用时经常出问题,而且只要出问题通常就是能导致程序崩溃的内 ...
- Jmeter实现WebSocket协议的接口和性能测试方法
WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex). 浏览器和服务器只需要要做一个握手的动作,然后,浏览器和服务器之间就形成了一 ...
- C语言初学 比较三个数中最大值的问题
#include<stdio.h> #include<math.h> main() { int x,y,n,m ,z; scanf("%d%d%d",&am ...
- cad 画 tp图
本文记录了cad 绘制 结构图 和触摸屏激光图. 1作为一个新手,我先要做的是适应操作界面. 页面:ctrl+鼠标滚轮 (类比,ps,ad等软件都是这样的.) 选取,划线,图层.等... 2画图... ...
- 设置iOS项目BuildVersion自动增加-备用
一.概念阐述:Build与Version的区别 在iOS中有两种“版本号”,也就是所谓的version号与build号,如下图所示: 我们用最简洁的语言来区分这两个版本号的区别以及用途如下: Vers ...
- 基本套接字总结(@function)
最近学习了下UNIX下的网络编程.为了以后查询方便,总结在这里. 首先套接字的地址定义: IPv4地址和IPv6地址定义见<netinet/in.h>头文件定义.为了能够顺利转换不同的套接 ...
- 【转】使用 vim + ctags + cscope + taglist 阅读源码
原文网址:http://my.oschina.net/u/554995/blog/59927 最近,准备跟学长一起往 linux kernel 的门里瞧瞧里面的世界,虽然我们知道门就在那,但我们还得找 ...
- 【转】ubuntu12.04完美安装QQ2012、QQMusic、Foxmail等--wine
原文网址:http://blog.csdn.net/hanmengaidudu/article/details/17616921 其实在这之前,试过无数次的wine模拟,没有一次成功的,也不能说是不成 ...
- linux/unix 段错误捕获【续】
本文为“在C/C++中捕获段错误,打印出错的具体位置”的续篇,进一步解决涉及动态链接库的情况. 背景知识: ·linux/unix下动态链接库的基本原理 ·/proc/pid/maps文件的基本格 ...
- C++ 中 struct和class 的区别
来自:http://hi.baidu.com/pengxiangbobin19890125/blog/item/b05586eee77300212df53411.html C++ prime 中 ...