POJ 1064 Cable master

  一开始把 int C(double x) 里面写成了  int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa。

后来发现又把二分查找的判断条件写错了,wa了n次,当 c(mid)<=k时,令ub=mid,这个判断是错的,因为要找到最大切割长度,当满足这个条件时,可能已经不是最大长度了,此时还继续缩小区间,自然就wa了,(从大到小递减,第一次满足这个条件的值,就是最大的值),正确的判断是当 c(mid)<k时,令ub=mid,这样循环100次时,自然能达到精度,100次循环能达到的精度是10^(-30);

  然后就是精度问题了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={,,-,},dy[]={,,,-};
const double INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; double a[];
int n,k; //k为应该切割的绳子段数
int C(double x){ //计算切割成长度为x的绳子时能得到的绳子的段数(注意x为double)
int ans=;
rep(i,,n){
ans+=floor(a[i]/x);
}
return ans;
}
int main(){
in2(n,k);
rep(i,,n){
inlf(a[i]);
}
double lb,mid,ub;
lb=0.0,ub=INF;
for(int i=;i<;i++){ //100次循环可以达到10^(-30)精度
mid=(lb+ub)/2.0; //二分查找
if(C(mid)<k) ub=mid; //切割的段数不够,要减小切割长度,此处不是C(mid)<=k
//(因为要找到第一次等于k的值,此时若等于k,mid可能不是最大的值)
else lb=mid; //切割段数满足,长度过小,要增大切割长度
}
printf("%.2f\n",floor(ub*)/);//一定要加floor函数,不加floor函数是四舍五入
return ;
}

  由于ub这个值是稍大于x的,最后用floor(ub*100)/100进行截取,直接截取不加floor函数是四舍五入。

POJ 1064 Cable master(二分查找+精度)(神坑题)的更多相关文章

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

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

  2. POJ 1064 Cable master (二分法+精度控制)

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 65358   Accepted: 13453 De ...

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

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

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

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

  5. poj 1064 Cable master 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1064 题解 二分即可 其实 对于输入与精度计算不是很在行 老是被卡精度 后来学习了一个函数 floor 向负无穷取整 才能ac 代码如下 ...

  6. POJ 1064 Cable master (二分)

    题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少. 析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整. 代 ...

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

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

  8. 二分搜索 POJ 1064 Cable master

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

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

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

随机推荐

  1. PHPCMSV9 更换域名后,要做的操作

    修改/caches/configs/system.php里面所有和域名有关的,把以前的老域名修改为新域名. 进入后台设置-->站点管理,对相应的站点的域名修改为新域名. 点击后台右上角的&quo ...

  2. cxf所用的lib

    cxf_lib

  3. GDI相关函数

    GetWindowRect计算窗口大小 MoveWindow 设置窗口大小 SetMapMode 该函数设置指定设备环境的映射方式 MM_LOMETRIC:每个逻辑单位转换为0.1毫米,X正方向向右, ...

  4. MVC中修改报错

    修改的时候有空值传入.

  5. MVC route 和 Angular router 单页面的一些方式

    直接看代码和注释吧 ASP.NET MVC router public class RouteConfig { public static void RegisterRoutes(RouteColle ...

  6. ngRoute 路由

    做单页面应用多是通过不同的url来识别出不同的页面展现的. angularjs 为我们提供一个封装好的ngRoute工具 简单介绍用法 : <div ng-view></div> ...

  7. Android开发程序获取GPS信息步骤

    1.获取LOCATION_SERVICE系统服务.2.创建Criteria对象,调用该对象的set方法设置查询条件.3.调用LocationManager.getBestProvider(Criter ...

  8. CreateFile FileSeek FileRead 直接读取数据

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  9. Why SignalR does not use WebSockets?

    Why SignalR does not use WebSockets?   As you probably know SignalR supports multiple transports. Th ...

  10. Smarty include使用

    {include} {include}用于载入其他模板到当前模板中. 在包含模板中可用的变量,载入后在当前模板仍然可用. {include}必须设置file 属性,设置载入的文件资源路径. 设置了可选 ...