POJ 1064 Cable master(初遇二分)

题目链接:http://poj.org/problem?id=1064

题意:有n条绳子,他们的长度是Li,如果从他们中切割出K条长度相同的绳子,这相同的绳子每条有多长,输出至小数点后两位

“ then the output file must contain the single number "0.00" (without quotes).”不是四舍五入到两位,一般四舍五入题目会说“bounded to”的提示

显然想得到的绳子越短就越能得到,绳子太长就得不到,二分搜即可

//236K	94MS
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n,k;
double a[10100];
bool judge(double x)
{
    int cnt=0;
    for(int i=1;i<=n;i++){
        cnt+=a[i]/x;
    }
    return cnt>=k? 1:0;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
    double lb=0,ub=100100;
    while(ub-lb>1e-5){
        double mid=(lb+ub)/2;
        if(judge(mid)){
            lb=mid;
        }
        else{
            ub=mid;
        }
    }
    printf("%.2f\n",floor(ub*100)/100);
    return 0;
}
时间: 2024-10-12 12:55:21

POJ 1064 Cable master(初遇二分)的相关文章

POJ 1064 Cable master 浮点数二分

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

poj 1064 Cable master ,二分 精度!!!

给出n根绳子,求把它们切割成K条等长的绳子的最大长度是多少? 二分 用 for(int i=0; i<100; ++i) 代替   while(r-l>eps) 循环100次精度能达到1e-30,基本上能一般题目的精度要求. 而 浮点数二分区间的话容易产生精度缺失导致死循环. #include<cstdio> double L[10000 + 10]; int n, k; int ok(double x) { int cnt = 0; for(int i=0; i<n; ++

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

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,这样循环1

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

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

POJ 1064 Cable master 【二分答案】

和杭电那一题一样,只不过G++交不能通过,C++能过 wa了好多好多好多次----------------------------------------- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8

二分搜索 POJ 1064 Cable master

题目传送门 1 /* 2 题意:n条绳子问切割k条长度相等的最长长度 3 二分搜索:搜索长度,判断能否有k条长度相等的绳子 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 1e4 + 10; 12 const int INF = 0x3f3f

POJ 1064 Cable master (二分 分数化整数)

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

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

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

POJ 1064 Cable master(很好玩的二分搜索)

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