poj Cable master (二分+精度)
2016-05-18 20:32
141 查看
Cable master
Description
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants 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
The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed 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
Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly 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
Sample Output
2.00
题意:给n个一定长度电线缆,要得到相等的k段,问最大每段可以是多长 。
二分,开始想着不难,但是精度啊。。。弄了一下午。。。。还有里面的浮点,整型的替换,这次不记住都不行,还有是四舍不五入!!!!算个技巧吧!!值得一记!!
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35226 | Accepted: 7504 |
Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organize the most honest contest ever. It was decided to connect computers for the contestants 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
The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed 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
Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly 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
题意:给n个一定长度电线缆,要得到相等的k段,问最大每段可以是多长 。
二分,开始想着不难,但是精度啊。。。弄了一下午。。。。还有里面的浮点,整型的替换,这次不记住都不行,还有是四舍不五入!!!!算个技巧吧!!值得一记!!
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> using namespace std; double a[10010]; int n,k; int judge(double x)//判断需要满足的条件 { int ans=0; for(int i=0;i<n;i++) ans+=(int)(a[i]/x); //!!!记住用法!记住啊!数据类型强制转换 if(ans>=k) return 1; else return 0; } int main() { while(~scanf("%d%d",&n,&k)) { double max=0.0; for(int i=0;i<n;i++) { scanf("%lf",&a[i]); if(max<a[i]) max=a[i]; } double left,right,mid; left=0.0; //二分的范围 right=max; while(right-left>1e-10)//最后求值有关精度问题,二分法的含义是找到左右逼近的数 { mid=(left+right)/2; if(judge(mid)) left=mid; else right=mid; } //输出right是根据题目的需要,来判断的 printf("%.2f\n",(int)(right*100)*1.0/100);//先强制转换再除 //用lf不行的,这也是个特殊情况吧。。。。 } return 0; }
相关文章推荐
- PHP处理密码方式
- final修饰符
- 【NOIP2013模拟】KC的陶器
- acm_Proud Merchants
- 如何用dx画一个界面
- jswindow.onload=function(){}与jquery $(function(){})
- 学习笔记:jQuery deferred对象的整理4
- 事件处理模型
- Django实例----简单的博客
- Android项目开发时常用到的工具类编写
- jdbc一篇小记录
- ASP.NET MVC 模块与组件(一)——发送邮件
- Java-文件输入输出(字节流)
- 百度图片爬虫-python版-如何爬取百度图片?
- Android中的文本框,图片以及点击事件的设置
- DOMNodeRemoved事件 删除元素之后触发的事件
- Matlab显示图像问题,double处理后,图像变白
- ssl例2
- 53.二叉树的下一个结点
- 转载:我的读博经历回顾与经验分享