POJ1064 Cable master(二分)

本题用二分搜索能够非常easy的求出答案。设条件C(X)为能够得到K条长度为X的绳子,C(x)=(floor(L(i)/x))。X的初始范围为(0,Max(L(i))+1)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

double a[10005];
int n,k;

void solve(double l,double r)
{
    double x;
    for(int j=0;j<100;j++)
    {
        x=(l+r)/2;
        int ncount=0;
        for(int i=0;i<n;i++)
        {
            ncount+=(int)(a[i]/x);
        }
        if(ncount>=k) l=x;
        else r=x;
    }
    printf("%.2f\n",floor(r*100)/100);
}

int main()
{
    //freopen("d:\\Test.txt","r",stdin);
	double Max;
	cin>>n>>k;
	for(int i=0;i<n;i++)
	{
	    scanf("%lf",&a[i]);
	    Max=max(Max,a[i]);
	}
	solve(0,Max+1);
	return 0;
}
时间: 2024-08-10 02:12:20

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

poj1064 cable master(最大值问题:二分+贪心)

题意: 有n条电缆,他们的长度分别为l[i].如果从n条电缆中切割出K条长度相同的电缆的话,这k条电缆每条最长能多长?答案小数点后保留两位有效数字. 输入: n, k n行:l[i] Sample Input 4 11 8.02 7.43 4.57 5.39 Sample Output 2.00 数据范围: 1<=N<=10000; 1<=k<=10000; 1<=l[i]<=100000. 分析: 设命题:can(x)=能切割出k条长度为x的电缆. 问题转化:求can

hdu 1551 Cable master(二分)

Cable master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2003    Accepted Submission(s): 751 Problem Description Inhabitants of the Wonderland have decided to hold a regional programming co

(poj)1064 Cable master 二分+精度

题目链接:http://poj.org/problem?id=1064 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 conn

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

POJ1064 Cable master 【精度问题】

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

poj1064 Cable master

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

Cable master(二分)

Cable master Time Limit: 1000/500 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 30     Accepted Submission(s): 15 Special Judge Description Inhabitants of the Wonderland have decided to hold a regional programmin

【自用】POJ1064 Cable master 且来说说卡精度的心得

广告: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44347241"); } 题意: 多组数据,n个小棒,分成m段,最长多长? 不能短于0.01,如果分不出来,输出"0.00" 题解: 满足单调性,来二分吧. 心得: 来,我们看着代码说话. 判无解的处理 首先最多能

POJ 1064 Cable master (二分)

题意:给定 n 条绳子,它们的长度分别为 ai,现在要从这些绳子中切出 m 条长度相同的绳子,求最长是多少. 析:其中就是一个二分的水题,但是有一个坑,那么就是最后输出不能四舍五入,只能向下取整. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include &l