UVA 12097 Pie

本文探讨了一道关于如何公平分配不同大小圆形派的问题,采用二分查找法求解最大可能的派体积,确保每人分得相同体积的派。讨论了二分法的实现细节及注意事项。

思路

给定N块半径分别为ri(i=1,2,..,N)的派,问若分给包括自己在内的F+1个人,每个人能分到的派最大是多少?

错误的思路是以整数的形式考察r,这将得不到所有的结果,以浮点数的形式考察的思路也是不明晰的,每次的增减标准量应该设为多少?

那么直接考察派的大小,可以尝试使用二分法去撞出合理的值。这里,简要的描述二分法。

当左边的值相离右边的值的时候
    取左右的中间值尝试
    如果派不够分
        将右值靠近
    否则
        将左值靠近

这里有一个二分法要注意的地方,中间值的计算比如h = (l + r) / 2,在本题是不可能溢出的,如果值过大呢?l + r将会溢出。这里应该h = l + (r - l) / 2

另外对于本题使用了浮点数更要考虑二分中比较左右时精度的问题,这里取EPSILON = 1e-5。具体的取多大合适可以研究。

还有一件事..PI不建议直接描述其值..3.1415926535获得了WA3.14159265358获得了AC。可以考虑使用数学库里的三角函数。

代码

#include <cstdio>
#define PI 3.14159265358
int N, F;
double A[10005];

bool judge(double v)
{
    int s = 0;
    for (int i = 0; i < N; i++) s += A[i] / v;
    if (s > F) return true;
    return false;
}

void solve()
{
    double l = 0, r = PI * 1e8, h = 0;
    while (r - l > 1e-5) {
        h = l + ((r - l) / 2);
        if (judge(h)) l = h;
        else          r = h;
    }
    printf("%.4lf\n", r);
}

int main()
{
    int T, V;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &N, &F);
        for (int i = 0; i < N; i++) {
            scanf("%d", &V);
            A[i] = PI * V * V;
        }
        solve();
    }
    return 0;
}

题目

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.

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.

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.

Input

One line with a positive integer: the number of test cases. Then for each test case:

  • One line with two integers N and F with 1 ≤ N , F ≤ 10000: the number of pies and the number
    of friends.
  • One line with N integers ri with 1 ≤ ri ≤ 10000: the radii of the pies.

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 oating point number with an absolute
error of at most 10−3 .

Sample Input

3
3 3
4 3 3
1 24
5
10 5
1 4 2 3 4 5 6 5 4 2

Sample Output

25.1327
3.1416
50.2655
代码转载自:https://pan.quark.cn/s/8ce4326d996e 对于在 CentOS 7 系统中修改网卡配置文件后无法使设置生效的情况,经过实践验证,可以通过使用 nmcli 命令来进行调整。完成修改之后,需要重新启动虚拟机以使更改生效,这样操作流程即告完成。如果设置仍然无法生效,则表明虚拟机在启动过程中所获取的 IP 地址配置并非针对 eth0,此时可以对其它网卡的配置文件进行修改或将其移除。在 CentOS 7 系统中,网络配置的管理机制与早期版本存在差异,主要体现为采用了 Network Manager 服务来负责网络接口的管理。在某些情形下,尽管修改了 `/etc/sysconfig/network-scripts` 目录下的 `ifcfg-eth0` 文件,但网络配置却未能即时生效。此类问题的发生通常源于 CentOS 7 采用了不同于以往的配置读取方法。接下来将具体阐述如何借助 nmcli 命令来处理这一挑战。 以 root 用户身份登录系统并打开终端界面。nmcli 是 Network Manager 提供的命令行界面工具,它支持在命令行环境下执行网络连接的建立、编辑、查询及管理任务。针对修改 eth0 网卡配置的需求,可以遵循以下步骤进行操作: 1. 导航至 `/etc/sysconfig/network-scripts` 目录: ``` cd /etc/sysconfig/network-scripts ``` 2. 检查该目录内是否存在 `ifcfg-eth0.bak` 文件,该备份文件可能是先前调整配置时遗留下来的,若存在可能造成冲突。若发现该文件,可以选择将其删除: ``` [root@localhost netw...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值