Program C--二分

來源:互聯網
上載者:User

標籤:

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 di?erent.

 

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

 

題目大意:我有個生日宴會邀請了f個朋友,宴會有n塊餅,餅的高度都為1,分別給出n塊餅的面積,要使得我和f個朋友每個人所分得的餅的體積都一樣,

求每人分得的最大體積。

 

注意:每個人只能得到一塊餅且不能由兩塊或兩塊以上不同的餅組合而成。

 

分析:首先用求出各個餅的體積,再將他們相加求出總體積(V),用V除以總人數(f+1)每人就可以得到最大的餅,由於每人不能由兩塊及兩塊以上不同的餅組合而成,

所以需要將V/(f+1)二分,直到找到最大且最合適的值。

 

 

代碼如下:

 

 

#include <iostream>#include <cstdio>#include <cmath>double pi=acos(-1);const int maxn=10005;double a[maxn];int n,f;using namespace std;bool test(double x){ int num=0; for(int i=0;i<n;i++) { num+=int(a[i]/x); } if(num>=f) return true; else return false;}int main(){ int t,r; double max,v,left,right,mid; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&f); f=f+1; for(int i=0;i<n;i++) { scanf("%d",&r); a[i]=pi*r*r; v+=a[i]; } max=v/f; left=0.0; right=max; while((right-left)>1e-6) { mid=(right+left)/2; if(test(mid)) left=mid; else right=mid; } printf("%.4f\n",mid); }}

Program C--二分

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.