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条绳子,长度分别为L[i]。如果从他们中切割出k条长度相同的绳子的话,这k条绳子每条最长能有多长?(答案保留小数点后两位,规定1单位长度的绳子最多可以切割成100份)。
这是一道明显二分搜索题。现在先进行二分搜索题的思考步骤。
设条件C(x)=可以得到k条长度为x的绳子。
现在问题变成求满足C(x)条件的最大的x。在区间初始化的时候,只需使用INF做上界即可:
st=;
en=INF;
那么现在的问题就变为了如何高效的判断C(x)是否满足。
由于长度为L的绳子最多可以切割出floor(L/x)段长度为x的绳子,因子C(x)=floor(Li/x)的总和是否不小于k,他可以在O(n)的时间内判断出来。
AC代码:
#include<stdio.h>
#include<math.h>
#define INF 0x3f3f3f3f
int n,k;
double a[];
bool C(double mid)
{
int num=;
for(int i = ; i < n ; i++)
num+=(int)(a[i]/mid);
if(num>=k)
return ;
else
return ;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i = ; i < n ; i++)
scanf("%lf",&a[i]);
double st=,en=INF;
///重复循环,直到解的范围够小,/100次循环可以达到10-30的精度
for(int i = ; i < ; i++)
{
double mid=(st+en)/;
if(C(mid)==)
st=mid;
else
en=mid;
}
printf("%.2f\n",floor(en*)/); }

AC代码另解,实际也是一样的:

#include<stdio.h>
#include<math.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define eps 1e-10
using namespace std;
int n,k;
double a[];
bool C(double mid)
{
int num=;
for(int i = ; i < n ; i++)
num+=(int)(a[i]/mid);
if(num>=k)
return ;
else
return ;
}
int main()
{
scanf("%d%d",&n,&k);
double en=-;
for(int i = ; i < n ; i++)
{
scanf("%lf",&a[i]);
en=max(en,a[i]);
} double st=;
while(en-st>=eps)
{
double mid=(en+st)/;
if(C(mid)==)
st=mid;
else
en=mid;
}
printf("%.2f\n",floor(en*)/); }

floor()函数:向下整取函数,头文件<math.h>

#include <stdio.h>
#include <math.h> int main ()
{
printf ("floor of 2.3 is %.1lf/n", floor (2.3) );
printf ("floor of 2.6 is %.1lf/n", floor (2.6) );
printf ("floor of -2.3 is %.1lf/n", floor (-2.3) );
printf ("floor of -2.6 is %.1lf/n", floor (-2.6) );
return ;
}

输出:

floor of 2.3 is 2.0
floor of 2.6 is 2.0
floor of -2.3 is -3.0
floor of -2.6 is -3.0

Poj:1064 : :Cable master (假定一个解并判断是否可行)(二分搜索答案)的更多相关文章

  1. POJ_1064_Cable_master_(二分,假定一个解并判断是否可行)

    描述 http://poj.org/problem?id=1064 有n条绳子,长度分别为l[i].如果从它们中切割出k条长度相同的绳子的话,这k条绳子每条最长能有多少? Cable master T ...

  2. poj 1064 Cable master 判断一个解是否可行 浮点数二分

    poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...

  3. POJ 1064 Cable master(二分查找+精度)(神坑题)

    POJ 1064 Cable master 一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...

  4. 二分搜索 POJ 1064 Cable master

    题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...

  5. [ACM] poj 1064 Cable master (二分查找)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21071   Accepted: 4542 Des ...

  6. [ACM] poj 1064 Cable master (二进制搜索)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21071   Accepted: 4542 Des ...

  7. POJ 1064 Cable master (二分)

    题目链接: 传送门 Cable master Time Limit: 1000MS     Memory Limit: 65536K 题目描述 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长 ...

  8. POJ 1064 Cable master

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37865   Accepted: 8051 Des ...

  9. poj 1064 Cable master【浮点型二分查找】

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29554   Accepted: 6247 Des ...

随机推荐

  1. Angular25 组件的生命周期钩子

    1 生命周期钩子概述 组件共有9个生命周期钩子 1.1 生命周期的执行顺序 技巧01:测试时父组件传递对子组件的输入属性进行初始化操作 import { Component, Input, Simpl ...

  2. java类中final方法的作用

    不给子类复写这个方法.说明你已经知道这个方法提供的功能已经满足你要求,不需要进行扩展,并且也不允许任何从此类继承的类来覆写这个方法,但是继承仍然可以继承这个方法,也就是说可以直接使用 inline扩展 ...

  3. c语言学习笔记 if语句的条件判断

    可能经常会看到错误的if语句示范,比如这样的: if(a=6) { printf("hello"); } if语句块执行的条件是if条件的运算结果不是0则执行if语句块. a=6这 ...

  4. getopt两个模块getopt 和gun_getopt 的异同

    getopt的两个模块getopt和gun_getopt都可以接收参数,但是又有不同; 先看 getopt.getopt这个模块: import sys import getopt def main( ...

  5. tensorflow 中 feed的用法

    Feed 上述示例在计算图中引入了 tensor, 以常量或变量的形式存储. TensorFlow 还提供了 feed 机制, 该机制 可以临时替代图中的任意操作中的 tensor 可以对图中任何操作 ...

  6. 复习action委托

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  7. Part8-不用内存怎么行_我从内部看内存lesson1

  8. 当Linux用尽内存

    Mulyadi Santosa 也许你很少面临这一情况,但是一旦如此,你一定知道出什么错了:可用内存不足或者说内存用尽(OOM).结果非常典型:你不能再分配内存,内核会杀掉一个任务(一般是正在运行那个 ...

  9. Java 可变字符串StringBuilder/StringBuffer的区别

    public class StringBuilder_and_StringBuffer { private static long SystemTime(){ return System.curren ...

  10. Graph cut使用方法

    下载:http://www.wisdom.weizmann.ac.il/~bagon/matlab.html 1. 运行compile_gc.m 2.运行gc_example.m(必须同目录吗?!) ...