uva 12097 - Pie(二分,4级)

解决如何将多个不同大小的圆形蛋糕等分成若干份,确保每个人都能获得相同体积的一份,通过二分查找算法找到最大的可能体积。

Problem C - Pie

Time limit: 1 second

 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 floating 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
The 2006 ACM Northwestern European Programming Contest

思路:二分结果,判断是否能切出符合的块数


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const double pi=acos(-1.0);
const int mm=1e4+9;
const double mx=1e-6;
double f[mm];
int n,m;
bool judge(double mid)
{
  int ans=0;
  for(int i=0;i<n;++i)
    ans+=int(f[i]/mid);
  if(ans>m)return 1;
  else return 0;
}
double fabs(double x)
{
  if(x<0)return -x;
  return x;
}
int main()
{
 int cas;
 while(~scanf("%d",&cas))
 {
   while(cas--)
   {
     scanf("%d%d",&n,&m);
     for(int i=0;i<n;++i)
      {scanf("%lf",&f[i]);
       f[i]=f[i]*f[i]*pi;
      }
      double l=0,r=1e10,mid;
      while(fabs(r-l)>mx)
      {
        mid=(l+r)/2;
        if(judge(mid))l=mid;
        else r=mid;
      }
      ///cout<<(l+r)/2<<endl;
      printf("%.4lf\n",(l+r)/2);
   }
 }
}




<think>好的,我现在要回答用户关于GCC编译选项-pie-no-pie-static-pie的问题。首先,我需要回忆这些选项的作用和区别,并结合用户提供的引用内容来确保准确性。 用户提到的引用[2]和[4]提到了-fpie-fPIE以及链接时的-pie选项,这些与位置无关可执行文件有关。我需要明确这些选项之间的关系,以及如何与用户的问题中的-pie-no-pie-static-pie联系起来。另外,用户还提到了-static,这可能与-static-pie有关联。 首先,-pie选项用于生成位置无关的可执行文件(Position-Independent Executable),这类似于共享库的位置无关代码(PIC)。位置无关的可执行文件在加载时可以被放置在内存的任意位置,这有助于增强安全性,例如通过地址空间布局随机化(ASLR)。用户引用中提到,-fpie-fPIE需要在编译时使用,而链接时需要-pie选项。例如,使用gcc -fpie -pie来编译程序,两者缺一不可。 接下来是-no-pie选项,它应该是禁用位置无关可执行文件的生成,即生成传统的绝对地址代码的可执行文件。这在某些情况下可能需要,比如当某些代码或库不支持PIE时,或者调试时希望地址固定。 -static-pie选项可能结合了静态链接和PIE的特性。静态链接通常使用-static选项,生成不依赖动态库的可执行文件。而-static-pie可能允许静态链接的可执行文件仍然是位置无关的,这样既具备静态链接的独立性,又支持ASLR。不过需要确认GCC版本是否支持该选项,以及具体的用法。 接下来,我需要区分这三个选项的使用场景和编译、链接阶段的参数。例如,-pie需要在编译和链接时都指定,而-static-pie可能在链接时替换-static,并启用PIE特性。同时,还要注意不同选项之间的互斥性,比如-static和-pie是否可以同时使用,或者是否需要-static-pie来替代。 用户提供的引用中还提到-fpic和-fPIC用于生成位置无关代码的库,而-fpie-fPIE用于可执行文件。这解释了为什么-fPIE不能用于编译库,而只能用于可执行文件。这有助于区分这些选项的不同用途。 此外,用户的问题还涉及-static-pie,这可能是一个较新的选项,我需要确认它的存在和用法。查阅GCC文档,发现从GCC 8开始支持-static-pie选项,允许创建静态链接的PIE可执行文件,这在安全性要求高的环境中可能有用。 在组织回答时,需要分点说明每个选项的作用、使用场景、编译和链接时的参数,以及它们之间的区别。还需要举例说明如何正确使用这些选项,比如编译命令和验证方法。同时,结合用户提供的引用内容,特别是关于-fpie-pie必须一起使用的说明,确保回答的准确性。 最后,生成相关问题,帮助用户进一步理解相关主题,比如ASLR的影响、动态链接与静态链接的区别,以及如何验证可执行文件的类型等。</think>### **GCC编译选项`-pie`、`-no-pie`、`-static-pie`详解** --- #### **1. `-pie`(位置无关可执行文件)** - **作用**:生成位置无关可执行文件(Position-Independent Executable),支持地址空间随机化(ASLR),增强安全性[^2][^4]。 - **编译要求**: - 编译时需添加`-fpie`或`-fPIE`(生成位置无关代码)。 - 链接时需添加`-pie`(最终生成PIE格式可执行文件)。 - **典型用法**: ```bash gcc -fPIE -pie main.c -o app_pie # 编译+链接 ``` - **验证方法**: ```bash file app_pie # 输出应包含 "PIE executable" ``` - **特点**: - 动态链接依赖库(如glibc),但代码段和数据段可随机加载。 - 适用于需要安全防护的场景(如服务端程序)。 --- #### **2. `-no-pie`(禁用位置无关特性)** - **作用**:强制生成传统绝对地址代码的可执行文件,关闭ASLR支持[^2]。 - **使用场景**: - 调试时需固定地址(如分析内存布局)。 - 依赖未支持PIE的第三方库。 - **典型用法**: ```bash gcc -no-pie main.c -o app_no_pie ``` - **验证方法**: ```bash readelf -h app_no_pie | grep Type # 输出为 "EXEC (可执行文件)",而非 "DYN (共享对象)" ``` --- #### **3. `-static-pie`(静态链接的PIE可执行文件)** - **作用**:生成完全静态链接且支持ASLR的可执行文件(GCC 8+支持)。 - **特点**: - 静态链接所有依赖(包括glibc),无外部动态库依赖。 - 同时支持地址随机化(类似动态链接PIE)。 - **典型用法**: ```bash gcc -static-pie main.c -o app_static_pie ``` - **验证方法**: ```bash file app_static_pie # 输出为 "statically linked, PIE executable" ldd app_static_pie # 输出 "not a dynamic executable" ``` --- ### **三者的核心区别** | 选项 | 链接方式 | ASLR支持 | 依赖库类型 | 典型场景 | |---------------|------------|----------|------------|------------------------------| | `-pie` | 动态链接 | ✔️ | 动态库 | 安全敏感的动态链接程序 | | `-no-pie` | 动态/静态 | ❌ | 动态库 | 调试、兼容旧代码 | | `-static-pie` | 静态链接 | ✔️ | 静态库 | 需独立分发且高安全的嵌入式系统 | --- ### **使用注意事项** 1. **`-pie`与`-static`的冲突** 直接组合`-static -pie`会引发链接错误,需改用`-static-pie`: ```bash # 错误:静态链接不支持传统PIE gcc -static -pie main.c -o app # 报错 # 正确:使用专用选项 gcc -static-pie main.c -o app ``` 2. **符号冲突处理** 静态PIE可能因glibc内部符号冲突需要特殊处理(参考`-Wl,--start-group`)[^1]: ```bash gcc -static-pie -Wl,--start-group -lc -lpthread ... -Wl,--end-group main.c ``` --- ### **实战示例** **动态PIE程序**: ```bash gcc -fPIE -pie -o http_server http_server.c ``` **静态PIE程序**(需glibc静态库支持): ```bash gcc -static-pie -o embedded_app embedded_app.c ``` **禁用PIE的调试程序**: ```bash gcc -no-pie -g -o debug_tool debug_tool.c ``` --- ### **相关问题** 1. ASLR对程序性能有何影响? 2. 如何检查系统中哪些库支持PIE模式? 3. 静态PIE程序能否调用动态库函数? 4. `-fPIC`和`-fPIE`生成的位置无关代码有何区别?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值