HDU problem g

两道类似的题目,第一道是分电缆,第二道是分蛋糕

Problem 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 &quot;star&quot; 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.<br><br>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.<br><br>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.<br><br>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.<br>
 

Input
The input consists of several testcases. The first line of each testcase contains two integer numbers 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 centimeter and at most 100 kilometers in length. All lengths in the input are written with a centimeter precision, with exactly two digits after a decimal point.<br><br>The input is ended by line containing two 0's.<br>
 

Output
For each testcase write to the output 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.<br><br>If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output must contain the single number "0.00" (without quotes).<br>
 

Sample Input
 
 
4 11
8.02
7.43
4.57
5.39
0 0
 

Sample Output
 
 
2.00

题意:

n段电缆,问能否分成k段相同长度的电缆,电缆不能接,输出保留两位小数;

二分枚举,注意精度,不是四舍五入,直接取整(这个代码没用到),直接输出左边的就相当于取整了

code:

#include<bits/stdc++.h>
#define exp 1e-8
using namespace std;
double s[10010];
double sum;
int n,k;
void suan(double l,double r);
bool check(double x);
int main()
{
    while(~scanf("%d%d",&n,&k),n+k)
    {
        sum=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%lf",&s[i]);
            sum+=s[i];
        }
        suan(0,sum/k);
    }
    return 0;
}
void suan(double l,double r)
{
    double mid;
    while(r-l>exp)
    {
        mid=(l+r)/2;
        if(check(mid))
            l=mid;
        else
            r=mid;
    }
    printf("%0.2f\n",l);
}
bool check(double x)
{
    int cnt=0;
    for(int i=1;i<=n;++i)
        cnt+=(int)(s[i]/x);
    if(cnt>=k)
        return true;
    else
        return false;
}

Problem Description
My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N of them, of various tastes and of various sizes. F of my friends are coming to my party and each of them gets a piece of pie. This should be one piece of one pie, not several small pieces since that looks messy. This piece can be one whole pie though.<br><br>My friends are very annoying and if one of them gets a bigger piece than the others, they start complaining. Therefore all of them should get equally sized (but not necessarily equally shaped) pieces, even if this leads to some pie getting spoiled (which is better than spoiling the party). Of course, I want a piece of pie for myself too, and that piece should also be of the same size. <br><br>What is the largest possible piece size all of us can get? All the pies are cylindrical in shape and they all have the same height 1, but the radii of the pies can be different.<br>
 

Input
One line with a positive integer: the number of test cases. Then for each test case:<br>---One line with two integers N and F with 1 <= N, F <= 10 000: the number of pies and the number of friends.<br>---One line with N integers ri with 1 <= ri <= 10 000: the radii of the pies.<br>
 

Output
For each test case, output one line with the largest possible volume V such that me and my friends can all get a pie piece of size V. The answer should be given as a floating point number with an absolute error of at most 10^(-3).
 

Sample Input
 
  
33 34 3 31 24510 51 4 2 3 4 5 6 5 4 2
 

Sample Output
 
 
25.13273.141650.2655

题意:

n块蛋糕,分给f+1(包括自己)个人,每个人必须是一块整的,求每个人分的的蛋糕最大,保留四位小数

二分,起点是按照最大的平分,终点是全部的平分

#include<bits/stdc++.h>
using namespace std;
double pi=acos(-1.0);//求较精确pi的方法
double n[10010];
double maxn,sum;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        double N,F;
        cin>>N>>F;
        ++F;//还有自己
        int x;
        maxn=0,sum=0;//别忘了每次归零
        for(int i=1;i<=N;++i)
        {
            cin>>x;
            n[i]=pi*x*x;//计算
            sum+=n[i];
            if(n[i]>maxn)
              maxn=n[i];//寻找最大的
        }
        double l,r,mid;
        l=maxn/F;//下限
        r=sum/F;//上限
        int ans=0;
        while(r-l>0.00001)
        {
            ans=0;
            mid=(l+r)/2;
            for(int i=1;i<=N;++i)//判断是否每个人都能分到
              ans+=(int)floor(n[i]/mid);
            if(ans<F)//判断人数
              r=mid;
            else
              l=mid;
        }
        printf("%0.4lf\n",l);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值