Cable master(二分题 注意精度)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 26596 | Accepted: 5673 |
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
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
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条绳子,求能切割出的最大长度,保留两位小数。 设C(X)表示能否切割出长度为x的K条绳子。 二分枚举X就行了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int N,K;
double len[MS]; bool judge(double x)
{
int cnt=;
for(int i=;i<=N;i++)
cnt+=(int)(len[i]/x+EXP);
return cnt>=K;
} void solve()
{
double l=0.0,r=MS*10.0;
double mid;
for(int i=;i<;i++)
{
mid=(l+r)/;
if(judge(mid))
l=mid;
else
r=mid; // 注意这里是double。
}
printf("%.2lf\n",floor(mid*)/);
// 千万注意精度
// printf("%.2lf\n",mid); WA
} int main()
{
scanf("%d%d",&N,&K);
for(int i=;i<=N;i++)
scanf("%lf",&len[i]);
solve();
return ;
}
Cable master(二分题 注意精度)的更多相关文章
- (poj)1064 Cable master 二分+精度
题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...
- poj1064 Cable master(二分)
Cable master 求电缆的最大长度(二分法) Description Inhabitants of the Wonderland have decided to hold a region ...
- POJ 1064 Cable master | 二分+精度
题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...
- POJ 1064 Cable master (二分答案)
题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉 ...
- POJ 1064 Cable master (二分)
题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少. 析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整. 代 ...
- poj 1064 Cable master 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...
- [POJ] 1064 Cable master (二分查找)
题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- Cable master(好题,二分)
Cable master Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- C++builder XE10 终于支持类内变量初始化了
Win32终于支持类内变量初始化了,C++11 用bcc32C编译器 llvm CLang.还支持Unicode 中文汉字 变量名. 用经典的bcc32编译还是不支持! class TPerson ...
- Spark RDD概念学习系列之RDD的操作(七)
RDD的操作 RDD支持两种操作:转换和动作. 1)转换,即从现有的数据集创建一个新的数据集. 2)动作,即在数据集上进行计算后,返回一个值给Driver程序. 例如,map就是一种转换,它将数据集每 ...
- Left join 中On和Where的作用范围
SQL语句如下: SELECT * FROM 表1 LEFT JOIN 表2 ON 表1.id = 表2.id AND 表2.Name != 'ff'WHERE 表1.NAME != 'aa' Lef ...
- android 简易定时器
定时器 1.在android 应用开发当中,很多时候都要用到定时器,而要实现定时器更多的时候要用到两个类:Timer,和TimerTask 2.API对Timer的解释是:
- C:进制
进制.C语言内存分配 1.对于进制 10进制 (0 - 9)16进制 (0——9 A B C D E F)硬件中的高低电平(0 和 1表示)所以计算机用 二进制 机器语言就是由 0 和 1 组成的一 ...
- 用CSS让字体在一行内显示不换行(收藏)
当一行文字超过DIV或者Table的宽度的时候,浏览器中默认是让它换行显示的,如果不想让他换行要怎么办呢? 用CSS让文字在一行内显示不换行的方法 一般的文字截断(适用于内联与块): .text- ...
- httpclient发起https请求以及获取https返回内容
工作中的需要,使用Apache httpclient发起获取通过https返回的内容,试了很多网上的解决办法都不行,查阅了Apache httpclient的官方文档,最后终于找出解决方法,分享给需要 ...
- Flex文件读取报错
Flex文件读取 1.s:WindowedApplication <?xml version="1.0" encoding="utf-8"?> &l ...
- Swift学习笔记十五
自动引用计数(Automatic Reference Counting) 和OC一样,Swift用自动引用计数机制来跟踪和管理你应用程序的内存,大多数情况下,你不需要考虑自己管理内存,Swift会自动 ...
- (高精度运算4.7.27)UVA 10494 If We Were a Child Again(大数除法&&大数取余)
package com.njupt.acm; import java.math.BigInteger; import java.util.Scanner; public class UVA_10494 ...