题目:题目太长了!

https://vjudge.net/problem/POJ-1064

题意分析:给了你N根长度为小数形式的棍子,再给出了你需要分的棍子的数量K,但要求你这K根棍子的长度必须是一样长的。需要你求解出满足题意的最长可能的棍子长度。根据二分找最大值的应用写即可。

需要注意的是:

1.注意题目给的范围是All cables are at least 1 meter and at most 100 kilometers in length;

2.在写满足条件的函数时,对于int强制转换类型的应用,一定要注意后面的括号;(起初这里WA的)

3.因为是找最大值,所以用right;

4.100次循环的二分写法可以达到2^100,即可以达到10^30,足够了。

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
const int MAXN = 1e4+5;
int N, K;
double Data[MAXN]; bool fun(double x)
{
int cnt = 0;
for(int i = 0; i < N; i++)
cnt += (int)(Data[i]/x); //cnt += (int)Data[i]/x; WA
if(cnt >= K)
return true;
else
return false;
} void solve()
{
double left = 0, right = 100001;
double mid;
for(int i = 0; i < 100; i++)
{ mid = (left+right)/2;
if(fun(mid))
left = mid;
else
right = mid;
}
printf("%.2f\n", floor(right*100)/100);
} int main()
{
while(~scanf("%d %d", &N, &K) )
{
for(int i = 0; i < N; i++)
scanf("%lf", &Data[i]);
solve();
} return 0;
}

对于第二种二分写法,需要注意的是在用exp的时候,exp不能取太少,同时不要用小的左值去减右值,最好是用一个正值去和exp进行比较判断。

#include <cstdio>
#include <cstring>
#include <cmath>
#define exp 1e-5
using namespace std;
const int MAXN = 1e4+5;
double Data[MAXN];
int N, K; bool fun(double x)
{
int cnt = 0;
for(int i = 0; i < N; i++)
cnt += (int)(Data[i]/x);
return cnt >= K;
} void solve()
{
double left = 0, right = 100000, mid;
while(right - left > exp) // **while(left - right < exp)
{
mid = (left + right)/2;
if(fun(mid))
left = mid;
else
right = mid;
}
printf("%.2f", floor(right*100)/100);
} int main()
{
while(~scanf("%d %d", &N, &K))
{
for(int i = 0; i < N; i++)
{
scanf("%lf", &Data[i]);
}
solve();
} return 0;
}

  

  

  

POJ1064 Cable master 【二分找最大值】的更多相关文章

  1. poj1064 Cable master(二分)

    Cable master 求电缆的最大长度(二分法)   Description Inhabitants of the Wonderland have decided to hold a region ...

  2. 二分法的应用:POJ1064 Cable master

    /* POJ1064 Cable master 时间限制: 1000MS 内存限制: 10000K 提交总数: 58217 接受: 12146 描述 Wonderland的居民已经决定举办地区性编程比 ...

  3. (poj)1064 Cable master 二分+精度

    题目链接:http://poj.org/problem?id=1064 Description Inhabitants of the Wonderland have decided to hold a ...

  4. poj1064 Cable master

    Description Inhabitants of the Wonderland have decided to hold a regional programming contest. The J ...

  5. poj1064 Cable master(二分查找,精度)

    https://vjudge.net/problem/POJ-1064 二分就相当于不停地折半试. C++AC,G++WA不知为何,有人说C函数ans那里爆int了,改了之后也没什么用. #inclu ...

  6. POJ1064 Cable master(二分 浮点误差)

    题目链接:传送门 题目大意: 给出n根长度为1-1e5的电线,想要从中切割出k段等长的部分(不可拼接),问这个k段等长的电线最长可以是多长(保留两位小数向下取整). 思路: 很裸的题意,二分答案即可. ...

  7. POJ 1064 Cable master (二分答案)

    题目链接:http://poj.org/problem?id=1064 有n条绳子,长度分别是Li.问你要是从中切出m条长度相同的绳子,问你这m条绳子每条最长是多少. 二分答案,尤其注意精度问题.我觉 ...

  8. [POJ] 1064 Cable master (二分查找)

    题目地址:http://poj.org/problem?id=1064 有N条绳子,它们的长度分别为Ai,如果从它们中切割出K条长度相同的绳子,这K条绳子每条最长能有多长. 二分绳子长度,然后验证即可 ...

  9. POJ 1064 Cable master | 二分+精度

    题目: 给n个长度为l[i](浮点数)的绳子,要分成k份相同长度的 问最多多长 题解: 二分长度,控制循环次数来控制精度,输出也要控制精度<wa了好多次> #include<cstd ...

随机推荐

  1. Luogu 4254 [JSOI2008]Blue Mary开公司

    BZOJ 1568 学习了一波李超线段树. 大佬blog 这个东西专门用来维护插入一条线段和区间/单点的最大/最小值. 插入的时候讨论: 1.如果当前结点上没有线段,那么直接插入. 2.如果当前结点上 ...

  2. Django Rest Framework框架 ---- url控制器

    Django Rest Framework框架 ---- url控制器

  3. c# 常规验证基类

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

  4. App测试从入门到精通之性能测试

    好了,上节我们介绍了关于APP测试的功能测试方面一些细节.这一篇我们来介绍一下,关于APP测试过程中的性能测试参考要点,我们需要思考的如下: 响应时间 1.APP安装卸载的响应时间 2.APP各种功能 ...

  5. linux学习1----初涉linux

    linux因其稳定高效的特点,受到很多开发者的青睐,因此将其作为服务器的操作系统. 作为一名开发者,程序员,掌握了一定的linux知识和技巧,程序的开发部署和运行也有不小的帮助. linux由于其开源 ...

  6. 什么是Condition Number(条件数)?

    In the field of numerical analysis, the condition number of a function with respect to an argument m ...

  7. 编写高质量代码改善C#程序的157个建议——建议60:重新引发异常时使用Inner Exception

    建议60:重新引发异常时使用Inner Exception 当捕获了某个异常,将其包装或重新引发异常的时候,如果其中包含了Inner Exception,则有助于程序员分析内部信息,方便代码调试. 以 ...

  8. eclipse启动了tomcat,但是浏览器打不开欢迎页

    tomcat在eclipse中启动成功,主页却打不开 症状: tomcat在eclipse里面能正常启动,而在浏览器中访问http://localhost:8080/不能访问,且报404错误.同时其他 ...

  9. Async异步委托

    /// <summary> /// 异步委托 /// </summary> public delegate void AsyncHandler(); public static ...

  10. android library使用方法

    一.Android library使用情景 通用模块的重复使用,项目做多了,其实都是差不多,核心模块基本无需大的改动,需要改的只是核心模块上的业务功能而已. Java中可以打包成库,或者说,单纯的ja ...