标签:cstring poj1064 code can count iostream 范围 std main
本题用二分搜索能够非常easy的求出答案。设条件C(X)为能够得到K条长度为X的绳子,C(x)=(floor(L(i)/x))。X的初始范围为(0,Max(L(i))+1)。
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; double a[10005]; int n,k; void solve(double l,double r) { double x; for(int j=0;j<100;j++) { x=(l+r)/2; int ncount=0; for(int i=0;i<n;i++) { ncount+=(int)(a[i]/x); } if(ncount>=k) l=x; else r=x; } printf("%.2f\n",floor(r*100)/100); } int main() { //freopen("d:\\Test.txt","r",stdin); double Max; cin>>n>>k; for(int i=0;i<n;i++) { scanf("%lf",&a[i]); Max=max(Max,a[i]); } solve(0,Max+1); return 0; }
标签:cstring poj1064 code can count iostream 范围 std main
原文地址:http://www.cnblogs.com/lytwajue/p/6871929.html