POJ:1064-Cable master
Cable master
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 58613 Accepted: 12231
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
4 11
8.02
7.43
4.57
5.39
Sample Output
2.00
解题心得:
- 题意就是给你n条绳子,你要把这些绳子切成k条一样的长度的绳子,问怎么切割k条绳子最长。保留两位小书。
- 很清晰的是不可能按照公式什么的来计算长度,那么就只能枚举,普通枚举肯定会超时,二分枚举就可以很轻松的解决。有小数二分的时候就有两种情况,
- 第一种是先全扩大100倍,二分之后得到答案再除100。
- 第二种就是直接二分,可以设定一个eps(eps不能太小,否则可能因为精度的丢失陷入死循环),或者直接设定一个二分次数跑二分。
- 还有就是最后保留2个小数,为什么要先化整再取小数的问题,不懂的可以打印一下这个代码来看一下(printf(“%.2f”,1.99999);)
直接使用小数二分:
#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <math.h>
using namespace std;
const int maxn = 1e4+100;
int n,k;
double len[maxn],max_len;
void init() {
max_len = 0;
for(int i=0;i<n;i++) {
scanf("%lf",&len[i]);
max_len = max(max_len,len[i]);
}
}
bool checke(double each_len) {
int cnt = 0;
for(int i=0;i<n;i++) {
cnt += len[i]/each_len;
}
return cnt < k;
}
double bin_search() {
double l,r;
l = 0; r = max_len;
for(int i=0;i<100;i++) {//设定循环次数100次
double mid = (l + r) / 2;
if(checke(mid))
r = mid;
else
l = mid;
}
return l;
}
int main() {
while(scanf("%d%d",&n,&k) != EOF) {
init();
double ans = bin_search();
int temp = ans * 100;
ans = (double)temp*0.01;
printf("%.2f\n", ans);
}
return 0;
}
先化整再二分:
#include <algorithm>
#include <stdio.h>
using namespace std;
const int maxn = 1e4+10;
int len[maxn],k,n;
void init() {
for(int i=0;i<n;i++) {
double temp;
scanf("%lf",&temp);
len[i] = temp*100;
}
}
int binary_search() {
int l = 0;
int r = 1000000000;
while(r - l > 1) {
int mid = (l+r)/2;
int cnt = 0;
for(int i=0;i<n;i++) {
cnt += len[i]/mid;
}
if(cnt >= k)
l = mid;
else
r = mid;
}
return l;
}
int main() {
printf("%.2f",1.99999);
while(scanf("%d%d",&n,&k) != EOF) {
init();
int ans = binary_search();
double P = (double)ans/100.0;
printf("%.2f\n",P);
}
return 0;
}
POJ:1064-Cable master的更多相关文章
- (poj)1064 Cable master 二分+精度
题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...
- poj 1064 Cable master 判断一个解是否可行 浮点数二分
poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...
- 二分搜索 POJ 1064 Cable master
题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- Poj:1064 : :Cable master (假定一个解并判断是否可行)(二分搜索答案)
Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...
- [ACM] poj 1064 Cable master (二分查找)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
- POJ 1064 Cable master
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37865 Accepted: 8051 Des ...
- [ACM] poj 1064 Cable master (二进制搜索)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
- POJ 1064 Cable master (二分法+精度控制)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65358 Accepted: 13453 De ...
- POJ 1064 Cable master (二分查找)
题目链接 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. ...
随机推荐
- Azure进阶攻略 | VS2015和Azure,想要在一起其实很容易
下雨天,巧克力和音乐很配…… 大冬天,男神和捧在手里的奶茶很配…… 「驴牌」的包包,和女神的全部衣服都配…… 对于「王首富」,容易实现的小目标和一个亿是绝配…… …… 醒醒吧!!这些事情和每天只会写代 ...
- Homestead 中使用 laravel-mix 问题汇总
按照 laravel 官方文档在准备使用 laravel-mix 时遇到了很多问题,许多同学应该会遇到同样的问题,自己花了一些时间来解决这些问题,在此做个笔记帮助大家减少填坑的时间. 环境 larav ...
- IDEA中git的配置与使用
IDEA中git的配置与使用 1.介绍 git是目前非常流行的版本管理管理软件,因其具有分布式特点,越来越受到企业的欢迎.IDEA作为一款优秀的开发软件,其内部也提供了对git的支持. 2.下载并安装 ...
- html中如何使用python屏蔽一些基本功能
进行数据解析的理由不计其数,相关的工具和技巧也同样如此.但是,当您需要用这些数据做一些新的事情时,即使有“合适的”工具可能也是不够的.这一担心对于异类数据源的集成同样存在.用来做这项工作的合适工具迟早 ...
- failed: No module named 'catkin_pkg' Make sure that you have installed "catkin_pkg"
https://stackoverflow.com/questions/43024337/why-this-error-when-i-try-to-create-workspaces-in-ros# ...
- cnblog编辑Latex数学公式
Latex在线公式编辑器 http://www.codecogs.com/latex/eqneditor.php 1. 行内公式: code $ \sqrt{a^2} $ display $ \sqr ...
- April 29 2017 Week 17 Saturday
Every man is a poet when he is in love. 每个恋爱中的人都是诗人. It is said this saying was from Plato, the famo ...
- 【转】如何手动添加Android Dependencies包
在ADT16 之前可以在工程里面做关联,eclipse会在工程上自动添加ReferenceLibrary.新版本的ADT修改了第三方jar的导入方式,只需要在工程目录下新建libs文件夹,注意是lib ...
- Python IDE PyCharm的快捷键大全
Python IDE PyCharm的快捷键大全 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性) Ctrl + Alt + Space 快速导入任意类 Ctrl ...
- maven没有servlet(创建servlet后报错)
maven不能创建servlet 解决方案 方案一 在项目的iml进行指定根目录 <sourceRoots> <root url="file://$MODULE_DIR$/ ...