码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1064 Cable master 浮点数二分

时间:2014-05-09 22:05:40      阅读:432      评论:0      收藏:0      [点我收藏+]

标签:c++   poj   二分   acm   


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 organize the most honest contest ever. It was decided to connect computers for the contestants using a "star" topology - i.e. connect them all to a single central hub. To organize a truly honest contest, the Head of the Judging Committee has decreed to place all contestants evenly around the hub on an equal distance from it. 
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible. 
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled. 
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.

Input

The first line of the input file contains two integer numb ers N and K, separated by a space. N (1 = N = 10000) is the number of cables in the stock, and K (1 = K = 10000) is the number of requested pieces. The first line is followed by N lines with one number per line, that specify the length of each cable in the stock in meters. All cables are at least 1 meter and at most 100 kilometers in length. All lengths in the input file are written with a centimeter precision, with exactly two digits after a decimal point.

Output

Write to the output file the maximal length (in meters) of the pieces that Cable Master may cut from the cables in the stock to get the requested number of pieces. The number must be written with a centimeter precision, with exactly two digits after a decimal point. 
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number "0.00" (without quotes).

Sample Input

4 11
8.02
7.43
4.57
5.39

Sample Output

2.00

Source


题解

又是一个浮点数的二分题。题意是这样的,给n个木头棒子,然后他们的长度是l_i,分成k个等分,不允许边角料的拼接。求分成k等分之后的每份长度。

关键还是精度的问题。怎么说呢,因为浮点数的二分毕竟是与整数有区别的。这个区别又特别的恶心,就是搞不出来到底精度差在哪里,那后就不断的尴尬。。。

有一个解决这类浮点数二分的技巧,就是转化成整数的二分,同时新增一个res用于维护当前最大(小)限能够满足的题目要求的值。然后就是不断地更新左边界右边界,同时维护res就行了,当然最后返回res,确保精度。

这个题我放两份代码,分别是直接浮点数二分的(G++与C++分别提交均通过),还有整数二分的。

代码示例

浮点二分法
/****
	*@author    Shen
	*@title     poj 1064
	*/

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

const double eps = 1e-5;

int n, k;
double l, v[10005];
double maxa = 0;

bool test(double x)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += int(v[i] / x);
    return sum >= k;
}

double Bsearch(double l, double r)
{
    while (r - l > eps)
    {
        double mid = (r + l) * 0.5;
        if (test(mid))
            l = mid;
        else r = mid;
    }
    return r;
}

void solve()
{
    maxa = 0;
    for (int i = 0; i < n; i++)
    {
        scanf("%lf", &l);
        v[i] = l;
        maxa = max(maxa, v[i]);
    }
    double ans = Bsearch(0.0, maxa);
    ans = 0.01 * int(ans * 100);
    //G++
        printf("%.2f\n", ans);
    //C++
    //  printf("%.2lf\n", ans);
}

int main()
{
    while (~scanf("%d%d", &n, &k))
        solve();
    return 0;
}
整数二分法
/****
	*@author    Shen
	*@title     poj 1064
	*/

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

const double eps = 1e-5;

int n, k;
double l, v[10005];
double maxa = 0;

bool test(int x)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        sum += int(v[i] / x);
    return sum >= k;
}

int Bsearch(int l, int r)
{
    int res = 0;
    while (l <= r)
    {
        int mid = (r + l) / 2;
        //printf("l = %d, r = %d, mid = %d, res = %d.\n", l, r, mid, res);
        if (test(mid))
            res = max(res, mid), l = mid + 1;
        else r = mid - 1;
    }
    return res;
}

void solve()
{
    maxa = 0;
    for (int i = 0; i < n; i++)
    {
        scanf("%lf", &l);
        l *= 100.0;
        v[i] = l;
        maxa = max(maxa, v[i]);
    }
    int ans = Bsearch(1, int(maxa));
    printf("%d.%02d\n", ans / 100, ans % 100);
}

int main()
{
    while (~scanf("%d%d", &n, &k))
        solve();
    return 0;
}

POJ 1064 Cable master 浮点数二分,布布扣,bubuko.com

POJ 1064 Cable master 浮点数二分

标签:c++   poj   二分   acm   

原文地址:http://blog.csdn.net/polossk/article/details/25406653

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!