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. ...
随机推荐
- 服务器返回的14种常见HTTP状态码
当我们从客户端向服务器发送请求时 服务器向我们返回状态码 状态码就是告诉我们服务器响应的状态 通过它,我们就可以知道当前请求是成功了还是出现了什么问题 状态码是由3位数字和原因短语组成的(比如最常见的 ...
- ios上【点击select元素,输入框自动获得焦点的问题】
今天遇到了一个很奇怪的问题:在ios手机上,如果页面出现滚动条,点击select元素的时候,偶尔会出现 “ 页面上的某一个输入框自动获得焦点 “ 的问题. 这个问题困扰我好久,一直找不到答案,今天终于 ...
- javascript:json对象和json字符串的相互转换
json对象和字符串的相互转换 //使用json中的parser方法转换: var str='{"name":"fendouer", "age&quo ...
- 在C++Builder中定义事件的实现方法
++Builder是由Borland公司推出的一款可视化集成开发工具.C++Builder的集成开发环境(IDE)提供了一系列可视化快速应用程序开发(RAD)工具,让程序员可以很轻松地建立和管理自己的 ...
- Python OOP 面向对象
1.Python实现OOP可以概括为三个概念: 继承:基于Python属性查找 多态:在x.method中,method的意义取决于x的类型 封装:方法和运算符实现行为,数据隐藏是一种惯例 2.委托: ...
- IOS Post请求(请求服务器)
@interface HMViewController () @property (weak, nonatomic) IBOutlet UITextField *usernameField; @pro ...
- 【BZOJ4487】[JSOI2015] 染色问题(高维容斥)
点此看题面 大致题意: 有一个\(n*m\)的矩形,先让你用\(C\)种颜色给它染色.每个格子可染色可不染色,但要求每行每列至少有一个小方格被染色,且每种颜色至少出现一次.求方案数. 高维容斥 显然题 ...
- 在TextBox控件中禁用鼠标右键
实现效果: 知识运用: MouseEventArgs类的Button属性 TextBox控件的ContextMenu属性 实现代码: private void textBox1_MouseDo ...
- 关于Linux部分版本无法安装Chrome的问题
在想要yum安装Chrome浏览器后发现安装没有相应的包,在查询后得知Chrome已经对Redhat和Centos等部分版本停止支持, 所以这些新版的系统中直接安装就显得有些困难了,那么从网上找到了一 ...
- webapi中配置返回的时间数据格式
web api返回的是标准格式UTC时间,如果要转成我们需要的格式,可以在WebApiConfig.cs的Register函数中新增以下配置来定义返回的时间类型格式: //配置返回的时间类型数据格式 ...