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. ...
随机推荐
- js屏蔽鼠标操作
document.body.onselectstart=document.body.oncontextmenu=function(){ return false;}
- Android无需权限显示悬浮窗
TYPE_TOAST一直都可以显示, 但是用TYPE_TOAST显示出来的在2.3上无法接收点击事件, 因此还是无法随意使用. 下面是我之前研究后台线程显示对话框的时候记得笔记, 大家可以看看我们项目 ...
- thinkphp的find()方法获取结果
find方法返回的是一行记录,结果是一个数组,数组的key和sql中的field相对应,假设: $res=$model->find(filed="a,b,c"); 获取结果中 ...
- ubuntu16.04安装中文输入法(转)
转自: https://zhidao.baidu.com/question/619127469641961052.html ubuntu没有预装中文输入法,需要自己安装 sudo apt instal ...
- Apache服务器开启gzip压缩的支持
为什么要在服务器上开启压缩?其实,服务器上开启压缩,对整个网站的就是在服务器上把网页的内容压缩后传给客户端,客户端解压后再显示网页的内容.实际就是增加了服务器端和客户端的工作量,减少了网络传输的数据量 ...
- 【JavaScript 封装库】BETA 3.0 测试版发布!
/* 源码作者: 石不易(Louis Shi) 联系方式: http://www.shibuyi.net =============================================== ...
- 问答 请问使用OK("raw:jpg")能返回多张图片吗
请问使用OK("raw:jpg")能返回多张图片吗 发布于 28天前 作者 qq_3aeeb0ad 78 次浏览 复制 上一个帖子 下一个帖子 标签: 无 @At( ...
- python 3+djanjo 2.0.7简单学习(三)--Django 管理页面
django里自带了一个管理页面,也就是后台,下面来学习一下 1.创建超级管理员 python manage.py createsuperuser 键入你想要使用的用户名,然后按下回车键: Usern ...
- 旧文备份: CANopen的LSS子协议中文翻译
有关节点地址和网络波特率的在线设置等:下载
- AngularJS 外部文件中的控制器
在大型的应用程序中,通常是把控制器存储在外部的文件中. <!DOCTYPE html><html><head><meta http-equiv="C ...