POJ1064 Cable master[二分]
来源:互联网 发布:南京行知中学地址 编辑:程序博客网 时间:2024/06/05 16:32
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
Output
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 118.027.434.575.39
Sample Output
2.00
题意:
给出绳子的个数N,将其分成等长的K条边,接下来N行给出第i条边的长度,求出切割出来的K条边最长能达到多少?
题解:
我们设目前当前切割出来的长度是x,而这个x的最大长度能达到N条绳子中最长的那一条,那么我们二分的范围就是从0到最长边,从中找到答案。(这题有点坑的是,结果只舍不进T_T)
double型的二分(精度弄得我好痛苦啊):
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<string>#include<stack>#include<set>#include<queue>#include<map>using namespace std;const int N=1e4+10;const double esp=1e-8;double num[N];double mx;int n,k;int check(double x){ int sum=0; for (int i=0 ; i<n ; ++i) sum+=(int)(num[i]/x); return sum>=k;}void Binary_search(){ double left=0,right=mx; while (left+esp<right) { double mid=(right+left)/2; if (check(mid)) left=mid; else right=mid; } int ans=right*100; printf("%.2f\n",ans/100.0);}int main(){ while (~scanf("%d%d",&n,&k)) { mx=0; double l; for (int i=0;i<n;i++) { scanf("%lf",&num[i]); mx=mx>num[i]?mx:num[i]; } Binary_search(); } return 0;}
int型的二分(这样看起来清晰多了):
#pragma comment(linker, "/STACK:102400000,102400000")#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<queue>#include<stack>#include<set>#include<map>#include<vector>using namespace std;typedef long long ll;const int N=1e4+5;int num[N];int n,k;int mx;bool can(int x){ int sum=0; for (int i=0 ; i<n ; ++i) sum+=(int)(num[i]/x); return sum>=k;}int solve(){ int left=1,right=mx,ans=0; while(left<=right) { int mid=(left+right)>>1; if(can(mid)) { left=mid+1; ans=mid; } else right=mid-1; } return ans;}int main(){ double x; while(~scanf("%d%d",&n,&k)) { mx=0; for (int i=0 ; i<n ; ++i) { scanf("%lf",&x); num[i]=x*100; mx=mx>num[i]?mx:num[i]; } int ans=solve(); int left=ans/100,right=ans%100; printf("%d.%02d\n",left,right); } return 0;}
- POJ1064 Cable master(二分)
- POJ1064 Cable master 二分
- POJ1064 Cable master 二分
- POJ1064 Cable master(二分)
- POJ1064 Cable master(二分)
- poj1064 Cable master(二分)
- POJ1064 Cable master[二分]
- POJ1064 Cable master(小数二分)
- POJ1064:Cable master(二分)
- Cable master poj1064(二分搜索)
- POJ1064 -- Cable master(二分)
- poj1064(二分)Cable master
- poj1064 Cable master(二分求解)
- POJ1064 Cable master(二分搜索)
- 【poj1064】cable master——二分
- POJ1064 Cable master(二分查找)
- poj1064 Cable master(二分搜索)
- POJ1064 Cable master(二分,精度控制)
- 《时代周刊》2016年度25项最佳发明出炉
- 常见防网站数据采集策略方法和它的弊端及采集对策
- javascript数值转换
- LeetCode 143. Reorder List
- Javascript扩展String.prototype实现格式金额、格式时间、字符串连接、计算长度、是否包含、日期计算等功能
- POJ1064 Cable master[二分]
- javascript词法结构
- oracle的number数据类型
- LeetCode 147. Insertion Sort List
- 第十六周 项目3-归并排序算法的改进
- wordpress实现文章阅读次数
- 数据结构(三) 单链表的排序 ----按插入方法排序(带头节点)
- JAVA基础知识--BigDecimal类
- Zend Framework 2 学习教程链接