Cable master poj1064(二分)

http://poj.org/problem?id=1064

题意:共有n段绳子,要求总共被分为k段。问在符合题意的前提下,每段长最大是多少?

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 11000;
typedef long long LL;
double a[maxn];
int n;

int Judge(double x)
{
    int ans = 0;
    for(int i=1; i<=n; i++)
    {
       ans += (int)(a[i]/x);
    }

    return ans;
}
int main()
{
    int  k;

    while(scanf("%d %d", &n, &k)!=EOF)
    {
        double ans = 0;
        for(int i=1; i<=n; i++)
        {
            scanf("%lf", &a[i]);
            ans = max(ans, a[i]);
        }

        double l = 0;
        double r = ans;

        while(r-l>1e-6)
        {
            double mid=(l+r)/2.0;

            if(Judge(mid)>=k) l=mid;
            else r = mid;
        }

        printf("%.2f\n",floor(r*100)/100);

        ///%.2f是四舍五入的,floor可以保证只舍不入
    }
    return 0;
}

时间: 2024-08-29 11:59:36

Cable master poj1064(二分)的相关文章

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(二分)

Cable master 直接上中文了 Descriptions 输入2个数 N  K n条绳子    要分成大于等于k段 求每段最长多长呢?并且每段不能小于1cm 必须以厘米精度写入数字,小数点后正好是两位数.如果无法切割所请求的每个长度至少为1厘米的件数,则输出文件必须包含单个数字“0.00”(不带引号). 多组文件输入 Sample Input 4 11 8.02 7.43 4.57 5.39 Sample Output 2.00 题目链接 https://vjudge.net/probl

Cable master(二分题 注意精度)

Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26596   Accepted: 5673 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(二分查找+精度)(神坑题)

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

Cable master(二分-求可行解)

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&

HDU 1551 Cable master【二分答案】

题意:给出n块木板,它们分别的高度,现在要把它们裁切成k块,问裁切成的最大的高度 二分答案,上限是这n块木板里面的最大值 然后每一个答案去判断一下是否满足能够裁切成k块 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map&

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