标签:
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 11178 | Accepted: 3899 | Special Judge |
Description
Input
Output
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
Source
题目大意:给出n个馅饼,m+1个人平均分这些馅饼,每人一块,问每个人最多分多少。
二分可以得到的馅饼大小。double二分
while( (high-low) > eqs )
{
mid = (low+high)/2.0 ;
if( solve(mid) )
{
low = mid ;
last = mid ;
}
else
high = mid ;
}
注意点:1 double 减法 (high - low)> eqs ,eqs用来卡精度,eqs太大精度降低,eqs太小时间变高。
2 二分的时候不加0.0001, 即low = mid high = mid;这样分可能会慢一点,但精度会高
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std ; #define PI 3.1415926535898 #define eqs 1e-5 double s[11000] ; int n , m ; double f(double x) { int k = (x+eqs) * 10000 ; x = k * 1.0 / 10000 ; return x ; } int solve(double x) { int i , j , num = 0 ; for(i = n-1 ; i >= 0 && (s[i]-x) > eqs ; i--) { j = s[i] / x ; num += j ; if( num >= m+1 )return 1 ; } if( num >= m+1 ) return 1 ; return 0 ; } int main() { int t , i , k ; double low , mid , high , last ; while( scanf("%d", &t) != EOF ) { while(t--) { scanf("%d %d", &n, &m) ; for(i = 0 ; i < n ; i++) scanf("%lf", &s[i]) ; sort(s,s+n) ; for(i = 0 ; i < n ; i++) s[i] = s[i]*s[i]*PI ; low = 0 ; high = s[n-1] ; while( (high-low) > eqs ) { mid = (low+high)/2.0 ; if( solve(mid) ) { low = mid ; last = mid ; } else high = mid ; } printf("%.4lf\n", last) ; } } return 0; }
标签:
原文地址:http://blog.csdn.net/winddreams/article/details/43053069