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. ...
随机推荐
- css3之移动平台资源
随着移动端越来越普及,前端技术也是百花齐放,但目前移动平台的技术已经趋向于成熟,记得刚实习的时候就是接触的移动端,但现在2年多来,期间遇到了很多莫名其妙的问题,见证了手机用户量的突飞猛进,兴喜的是更多 ...
- JS的函数参数传递为值传递
function setAge(i) { alert(i);//24 i = 18; alert(i);//18,i的改变不会影响外面的age }; var age = 24; setAge(age) ...
- Python列表边遍历边修改问题解决方案:alist[:]
最近在看python,遇到个简单的问题:删除列表中指定的重复元素,发现一些实用并且有趣的东西. 1.错误示范 alist = [1,1,2,2,3,3,2,2,1,1] for i in alist: ...
- JDBC中重要的类/接口-Connection、DriverManager、ResultSet、Statement及常用方法
DriverManager(管理一组 JDBC 驱动程序的基本服务) 它的方法: getConnection(String url, String user, String password) 试图建 ...
- centos6.5_64bit-Tomcat7安装部署
此次安装系统版本及软件版本 centos6.5-64bit java -1.7.0_45 jdk1.8.0_111 apache-tomcat-7.0.73 一.检查java版本信息 ...
- HTML-JS-CSS基础
HTML-JS-CSS基础 1.html hyper text markup language,超文本标记语言,所见即所得.web开发中用于展示功能的部分,浏览器可对其进行渲染.产生各种可视化组件,比 ...
- vos对接时业务确认及信息安全责任承诺书
业务确认及信息安全责任承诺书 双方达成一致,******网络科技有限公司向 有限公司提供中国移动网内语音线路接入服务,保证资源长期使用,保证线路资源接通率标 准,合 ...
- DEEP LEARNING 大满贯课程表
Reinforcement Learning post by ISH GIRWAN Courses/Tutorials Deep Reinforcement Learning, Spring 2017 ...
- PHP:__get()、__set()、__isset()、__unset()、__call()、__callStatic()六个魔术方法
哎呀呀,今天小仓鼠学到了魔术方法,简称魔法,哈哈哈哈,神经病啊~ 平时在面试的时候,也会遇到问魔术方法有哪些的问题哦!今天我们来了解一下下~ 1.__get() 形式: __get($objName) ...
- EF写distinct
在日常开发中常常是这么写的 var logErrorRequest = from l in _logErrorRepository.Table select new { WrongTime = l.W ...