Poj 1064 && Hdu 1551 Cable master【二分搜索】

HDU1551:Cable master(二分) 题意: 有n条绳子,分成k段相等的绳子,不要求全部用完,求能分最长的为多少 题目要求保留两位且不进位  用%.2lf直接输出时会四舍五入,所以需要向下取整。 注意这里答案是r不是l,因为cnt比k小是符合条件的,大于k就不符合条件了   //#include<bits/stdc++.h> #include<iostream> #include<cmath&... 阅读详情

Cable master
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 33802 Accepted: 7186

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



题意:

每组输入两个数,第一个数代表有多少东西,后边的数字是需要处理成多少部分,

让你求得,能满足分成这么多部分的最大长度,精度到小数点后两位..................


分析:

这个题是个无限逼近求最优的问题,可以采用二分法一直逼近求极限状态......


题解:

这个题除了输出数据的格式比较坑之外,还是不难的,只需要控制好二分的格式就好,

另外定义一个统计的函数,多次调用,当成二分的一个处理条件(看题意要求),也就是满足分成指定数目的部分.....


#include<stdio.h>
int n,m,i;
double y[10005];
int count (double x)
{
    int c=0;
    for(i=0;i<n;++i)
    {
        c+=(int)(y[i]/x);
    }
    return c;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        double sum=0.0;
        for(i=0;i<n;++i)
        {
            scanf("%lf",&y[i]);
            sum+=y[i];
        }
        double l=0,r=sum/m,mid;// r 首先是平均值,也是第一个可能的输出结果,假设全部没有剩余呢,是吧
        while(r-l>1e-10)
        {
            mid=(l+r)/2.0;
            if(count(mid)>=m)//这里只要满足要求,就一直增大左边的界限
            {
                l=mid;
            }
            else//只有不满足的时候才更新右界限,这样才可以保证 r 一直是满足题意而且最大的那个值
            {
                r=mid;
            }
        }
        r=(int)(r*100)/100.0;//这样舍去小数的确很巧妙!
        printf("%.2f\n",r);//!!!!!!!注意,改成.2lf 就会显示答案错误,已经验证过了,这是Hdoj 的问题!!!!!!!
    }
    return 0;
}



/*
2016年3月1日21:15 poj 提交 
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; 
int n,k;
double x[100005];
int num(double len)
{
	int cnt=0;
	for(int i=0;i<n;++i)
	{
		cnt+=(int)(x[i]/len);
	}
	return cnt;
}
int main()
{
	while(~scanf("%d%d",&n,&k))
	{
		double maxn=0;
		for(int i=0;i<n;++i)
		{
			scanf("%lf",&x[i]);
			maxn=max(maxn,x[i]);//还是以最长的为最大值比较保险
		}
		double l=0,r=maxn,mid;
		for(int i=0;i<100;++i)//据说这样就足够了..
		{
			mid=(l+r)/2.0;
			if(num(mid)>=k)
			{
				l=mid;
			}
			else
			{
				r=mid;
			}
		}
		printf("%.2f\n",(int)(r*100)/100.0);
	}
	return 0;
}



《算法竞赛从入门到进阶》心得及相关题解14--dfs--hdu1551 题目意思:hdu1551,给你n条电缆及其长度,现在需要k条同样长度的电缆,问最多可以切割的长度。如给你4条电缆长度分别是8.02、7.43、4.57、5.39,现在需要11条同样长度的电缆,那么最多只能切割成2.00的长度。 解题思路:水题,书上写的dfs题目,实际上楼主用的是二分查收,查找的长度从电缆中最大的长度maxg/k至所有电缆的长度之和sum/k之间。 代码明细: //probID: hdu1551 //author: WiselyQY //date: 2021-01-03 #includ. 阅读详情

相关推荐

EMC系列EtherCAT总线运动控制器(二):Motion通讯连接、总线配置及诊断

雷赛-EMC系列EtherCAT总线运动控制器(二):Motion通讯连接、总线配置及诊断

weixin_38570076的博客 1938

hdu1551 简单二分

简单二分枚举

zxf654073270的专栏 816

嵌入式Linux网络调试:YT8531/YT8521 PHY驱动移植与设备树配置避坑指南

本文深入解析嵌入式Linux系统中YT8531/YT8521 PHY驱动移植与设备树配置的关键技术,涵盖内核驱动适配、RGMII接口时序调整及常见问题排查。针对Motorcomm千兆以太网PHY芯片,提供详细的设备树配置示例和调试技巧,帮助开发者规避典型设计陷阱,确保网络功能稳定运行。

weixin_42525632的博客 687

hdu1551-二分&细节-Cable master

https://vjudge.net/problem/HDU-1551 给定m个木棍,问你把这m个棍子中,分割成至少k个木棍。问能分割的最大长度是多少。 二分,经典的 最大化。 但是有一个细节,就是挑战上,是取的r作为结果,最后再取floor。(截取),这一点没看懂qwq。 而再网上看的其他代码,就是正常写。#include <cstdio> #include <cmath> using n

天与云与山与水 329

2.6 SST的核心性能指标体系

本文构建了固态变压器(SST)的核心性能指标体系,涵盖电气性能、可靠性、经济性和环境适配性四大类指标。电气性能指标包括运行效率(≥97%)、功率密度(≥900kVA/m³)、谐波畸变率(≤3%)等关键参数;可靠性指标设定MTBF≥43800h、年均故障率≤1.2次/年等标准;经济性指标对比分析了初始购置成本(45-55万元)和全生命周期成本(48-55万元);环境适配性指标则明确了温度、海拔等工况要求。该体系采用量化公式和国标限值,为SST的性能测试、选型和优化提供了统一标准。

技术工作者 623

从TM1到TM9:手把手教你用Wireshark抓包分析LTE下行调度与传输模式切换

本文详细解析了如何使用Wireshark抓包分析LTE下行调度中的传输模式切换,从TM1到TM9的动态调整过程。通过实战案例,包括小区边缘移动、高速场景和多用户调度,揭示eNodeB如何基于信道质量指标(如RSRP、SINR)动态切换传输模式,并提供关键信令解析和优化建议,助力网络工程师提升LTE网络性能。

weixin_42651748的博客 324

hdu1551题解(wa了好多次)

二分题,卡精度,刚开始我是先转化为整数做的,wa到死,破水题!!!#include <stdio.h> #include <stdbool.h> int N,K; double cable[10001]; bool is_ok(double size) { int i,sum=0; for(i=0;i<N;i++) sum+=(int)(cable[i]/size);

帷幕后的文和 469

储能电池管理系统介绍(一)

介绍储能BMS的架构以及各模块功能

manjixingkong1234的专栏 1936

(step4.1.4)hdu 1551(Cable master——二分法)

题目大意:在做过hdu1969的前提下,这道题其实不需要知道什么意思就可以做了。。。。哈哈,我就是这样的。。如果需要明白是什么意思的 看一下我的hdu1969的解题报告就行了 解题思路:二分法 1)对于二分法、三分法的相关题目。精度的大小是一个很重要的问题。如何确定呢。。。我们可以这样思考: 根据题目给定的范围来确定。。如本题中有这么一句话"All cables are a

黄俊东的专栏 1242

全功能tgbot/Telegram机器人多功能有后台版源码

trx兑换 闪兑 关键字监控 群管 usdt监控 余额查询 推广分享 等几乎常见的tg机器人功能都有。全功能tgbot/telegram机器人多功能有后台版源码-打包好的可以直接上手。

百创科技 2740

专栏二十:如何选择你的单细胞亚群的分辨率--ROGUE

ROGUE这是继chooseR后第二个计算单细胞亚群分辨率的内容,其实关于代码的运行很简单,网上已经有不少教程,重复这些就没意思了,这里主要是结合探讨一下这个算法。

m0_58549466的博客 987

POJ1064 HDU1551 ZOJ1464 LA2452 Cable master【二分搜索】

Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 89737 Accepted: 18228 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The Judging Committee has volunteered and has promised to organiz

海岛Blog 392

软考高级-系统架构设计师 论文范文参考(一)

系统架构设计师 论文参考

片言可决讼,半纸能缚龙 4373

HDU-1551 Cable master

二分搜索

Kiyonami Yu 361

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

hdu 1551

题意:给你n条电缆,分成k截,每截最长是多少? 找一个长度,判断它能产生多少截绳子。没错,二分法。 需要注意的是,输出的时候要小心四舍五入是不行的。(长度超长,不会产生相应的绳子) #include #include #include using namespace std; const int maxn = 10000 + 5; const double eps =

DickensTone的博客 401

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

HDU1551(二分)

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

晓风残月xj 1115

poj1064 Cable master(二分求解)

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 wa

j2013210855的专栏 551

HDU 1551(搜索题,二分)

Cable master<br />Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)<br />Total Submission(s): 257    Accepted Submission(s): 72<br /><br />Problem DescriptionInhabitants of the Wonderland have decided to hold a regional pr

-- bird -- 2378

HDU 1551(Cable master)

基础题,二分查找即可。 #include <iostream> #include <iomanip> using namespace std; const int MAXN = 10005; const double eps = 1e-6; double len[MAXN]; //每段绳子的长度 int N, K; //判断是否能得到K段长为x的绳子 bool check(double x) { int total = 0; for (int i = 0; i <

Intelligence1028的博客 374

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

HDU1551

n跳绳分成k段,问最长的分法 #include<cstdio> #include<iostream> #include<algorithm> #define eps 1e-7 #include<cmath> using namespace std; int n,m; double arr[100010]; bool jg(double x) { ...

Yuzzzzzz的博客 456

POJ 1064

题目链接:http://poj.org/problem?id=1064Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17163   Accepted: 3650 Description Inhabitants of the Wonderlan

起航 780

【HDU】1551 - Cable master(二分)

点击打开题目 Cable master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3559    Accepted Submission(s): 1355 Problem Description

wyg1997 762

HDU 1551(二分算法)

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 d...

zhe 350

hdu 1551 Cable master

#include #include using namespace std; int n,k; double len[10010]; /*int judge(double s) { int cnt=0; for(int i=1;i<=n;i++) { cnt+=(int)(len[i]/s); } if(cnt>=k) return 1; else return 0; }

星空永恒 402

hdu1551Cable master

题目大意: 多种不同长度的电缆线,要求你在这里面ge

追梦的Anker 682
上一篇: Poj 1631 && Hdu 1950 Bridging signals【LIS】
下一篇: 79 拦截导弹
hope1262946533
博客等级 码龄12年 107粉丝 826原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值