POJ1064 Cable master[二分]

POJ1064 Cable master 二分 题目链接:http://poj.org/problem?id=1064 题目大意:有n条绳子,长度分别为L[i]。如果从他们中切割出k条长度相同的绳子的话,这k条绳子每条最长能有多长?(答案保留小数点后两位,规定1单位长度的绳子最多可以切割成100份)。 分析:二分搜索最大长度x。我们令C(x)为可以得到K条长度为x的绳子,那么问题就变为了求满足条件C(x)的最大的x。在区间初 阅读详情

Cable master
Time Limit:1000MS     Memory Limit:10000KB

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条边,接下来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;
}


POJ——1064题 Cable master 二分搜索 Cable master Time Limit:1000MS Memory Limit:10000K Total Submissions:75537 Accepted:15336 Description Inhabitants of the Wonderland have decided to hold a regional programmin... 阅读详情

相关推荐

SAP BTP-Cloud foundry 构建并部署JAVA应用程序

本文为 SAP BTP Java 应用程序的简单实例, 我们可以用此实例来熟悉SAP BTP 中的java应用开开发流程.在你完成改示例java应用程序以后, 我们可以直接使用该java应用程序进行后续的开发流程, 或直接构建一个新的JAVA应用程序来支持新的业务开发。

seyafly的博客 803

二分搜索--poj1064

Language: Default Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18709   Accepted: 4007 Description Inhabitants of the Wonderland have de

龙崎的专栏 999

app逆向分析案例-95分

先把justtrusmeplus打开设置好95分,开始抓包,得到结果为: https://www.95fenapp.com/api_goods/list/v3.0?is_all=1&keywords=%E7%A7%91%E6%AF%94& page=1&page_size=20 &publish_timestamp=0 &scene=2&scene_type=95fen_android_search_personal&sn=SearchList

weixin_41763171的博客 2741

HDOJ 1551 Cable master(二分查找)

Cable master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2513    Accepted Submission(s): 940 Problem Description Inhabitants of t

Chunzhen的博客 832

【实战指南】WinPE环境下360系统急救箱的病毒查杀全流程解析

本文详细解析了在WinPE环境下使用360系统急救箱进行病毒查杀的全流程。从制作WinPE启动盘到进入PE环境,再到360急救箱的深度使用和系统修复,提供了实用技巧和常见问题解决方案,帮助用户高效清除顽固病毒。

weixin_28568819的博客 534

从音频录制到智能手表:Sigma-Delta ADC是如何在低功耗场景下‘欺骗’噪声的?

本文深入解析Sigma-Delta ADC在智能穿戴设备中的低功耗高精度实现原理,重点介绍其通过过采样和噪声整形技术‘欺骗’噪声的独特方法。以TWS耳机和医疗级健康监测为例,展示该技术如何在有限功耗预算下实现专业级信号采集,为消费电子设计提供关键解决方案。

weixin_30825581的博客 388

140_割绳子问题 Cable master (POJ No.1064)

有N条绳子,他们的长度分别为L_i。如果从他们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多少?  用二分搜索法就可以解决,重点是他要规定收敛次数,而不是收敛的差值。规定收敛差值太小的话,可能会导致收敛困难。 // // 140_cable master.cpp // changlle // // Created by user on 1/8/16. // Copyr

Bernini @ buffalo 1247

嘉立创EDA实战:4层板叠层设计避坑指南(附阻抗计算技巧)

本文详细解析了嘉立创EDA在4层PCB叠层设计中的实战技巧与避坑指南,涵盖经典叠层方案对比、介质厚度选择、电源系统设计及高速信号完整性优化。特别介绍了阻抗计算工具的应用,帮助工程师在高速电路设计中实现性能与成本的最佳平衡。

sea99的博客 961

POJ1064-Cable master

题目出的也是十分的坑。 数据范围从1meter到100kilometers.... 解题的思路大致就是二分搜索,次数大概在100次左右。 答案输出需要floor(ans*100)/100来避免最后答案四舍五入。 #include #include const int maxn = 10000; double cab[maxn]; bool judge(double a, int

Collider 485

基于Simulink的PMSM轮毂电机崎岖路面悬架协同控制仿真

本文完成了基于Simulink的PMSM轮毂电机崎岖路面悬架协同控制仿真,实现了:✅ 构建了四轮独立驱动+主动悬架高保真整车模型✅ 实现了Skyhook主动悬架控制✅ 设计了驱动-悬架状态共享与协同决策机制✅ 验证了5 m/s高速下连续颠簸路面稳定通过能力✅ 为高性能移动机器人提供一体化底盘解决方案。

xiaoheshang_123的博客 189

块存储:AIO的直接写流程注释

提交io流程中aio_write之前函数的注释,可参考“块存储:AIO的直接读流程注释”。

yiyeguzhou100的专栏 1333

Cable master 小数二分

二分专题 保留两位小数 陷阱

淘萄桃的博客 2168

8.9华为OD机试真题 新系统 - 灯带颜色变换 (Java/Py/C/C++/Js/Go)

8.9华为OD机试真题 新系统 - 灯带颜色变换 (Java/Py/C/C++/Js/Go)

算法大师! 663

POJ_1064_Cable master【二分】

/* Cable master Time Limit: 1000MS        Memory Limit: 10000K Total Submissions: 43878        Accepted: 9409 Description Inhabitants of the Wonderland have decided to hold a regional programmi

持之以恒_fighting 1831

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

poj 1064 二分 Cable master

题目链接:    http://poj.org/problem?id=1064 Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31403   Accepted: 6671 Description In

世靖的码场 745

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

POJ1064 Cable master 二分

DescriptionInhabitants 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 dec

Bill_Utada的成长之路 1004

POJ 1064 Cable master(二分)

POJ 1064 Cable master(二分) 题意:现在有N个绳子,需要这N条绳子切割成K段,问长切割方案是什么. 思路:因为长度的范围在[1.0, 100km]之间,所以不可能去枚举一个长度,只能去二分一个长度并且判断该N个绳子截取该长度能截取的个数. /*************************************************************...

dijingzhou4259的博客 106

POJ 1064 Cable master (二分)

题目链接:http://poj.org/problem?id=1064 solution:二分法、代码比较简单 #include <iostream> using namespace std; #include <iomanip> #include <cmath> int n, k; double l[10000]; //判断是否满足条件 bool che...

weixin_44014982的博客 206

poj1064Cable master【二分】

Language: Default Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32143   Accepted: 6881 Description Inhabitants of the Wonderland have deci

走一步再走一步 904

POJ 1064 Cable master 二分答案

题目:http://poj.org/problem?id=1064题意:给n段绳子和各自的长度,长度均精确到小数后两位,现在把n段绳子切成k段等长的绳子,求最大长度思路:二分答案,枚举长度,判断能否切成k段。这个题精度问题真是一大坑点,因为长度均精确到小数后两位,于是可以把所有绳子扩大100倍为整数,然后去做,这样就可以避免精度问题。直接用浮点数去二分的话,二分多少次是问题,另外注意输出的时候是直接

弱智就要多努力 996

【poj1064】cable master——二分

题目: Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33548   Accepted: 7154 Description Inhabitants of the Wonderland have decided to hold a

CFhM_R's Blog 395

POJ 1064 Cable master(二分答案)

嗯... 题目链接:http://poj.org/problem?id=1064 其实这是一道很好想的二分答案的一道题... 二分的区间就是1~max_l,从1开始是因为所有小于1的都需要按0计算,没必要讨论了... 每次二分出来的答案看它能否把电缆切成大于等于k块,如果可以,我们不能保证它是最优的答案,所以要向更大的地方二分;如果现在都不可以,我们必须向更小的...

weixin_30871905的博客 233

二分入门——poj 1064 cable master

这道题是一道很标准的二分入门题,从这道题中可以感受到二分法在精度问题上强大的处理能力

Fente_Silence的博客 439

POJ 1064 Cable master 【二分】

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37400   Accepted: 7940 Description Inhabitants of the Wonderland have decided to hold a region

博客已搬迁到http://www.cnblogs.com/cniwoq/ 433

poj1064 Cable master 二分处理浮点数

http://poj.org/problem?id=1064 floor返回的值是浮点型,而(int)返回的值是整型。 int是向0取整,比如:1.9会变成1,-1.9会变成-1。 floor是向下取整,比如:1.8会变成1,-1.1会变成-2(注意这点和int不同)。 #include &lt;iostream&gt; #include &lt;cmath&gt; #include &...

skykone1的博客 684
上一篇: POJ1064 Cable master[二分]
下一篇: ZOJ3203 Light Bulb[三分/推公式]
ControlBear
博客等级 码龄11年 34粉丝 51原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值