链接:
https://vj.e949.cn/889526602f1f7ab5e44d789392754748?v=1539700751
题意:
水池按从高到低分别给出容量,同时注水,满了可以往下溢出,问全部水池都满的时间和最低水池满的时间是多少。
思路:
二分时间,对于每个时间,判断是否满。
代码:(一定要用scanf啊!!!!!!!)
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
double cap[maxn];
double flow[maxn],f;
int n;
bool judge(double t,int flag )
{
for(int i = 0 ; i < n; i++)
flow[i] = t * f;
for(int i =0 ; i < n - 1; i++)
{
if(flow[i]>cap[i]) flow[i+1] += flow[i] - cap[i];
else if(flag) return false;//判断所有满的情况
}
return flow[n-1] > cap[n-1];
}
int main()
{
while(scanf("%d%lf",&n,&f)==2)
{
for(int i = 0; i < n; i++)
scanf("%lf",&cap[i]);
double ans1,ans2;
double l = 0,r = 1e9 + 10;
for(int i = 0; i < 80; i++)
{
double mid = (l+r)/2.0;
judge(mid,0)? r = mid : l = mid;
}
ans1 = l;
l = 0;
r = 1e9+10;
for(int i = 0; i < 80; i++)
{
double mid = (l+r)/2.0;
judge(mid,1)? r = mid : l = mid;
}
ans2 = l;
printf("%.8f %.8f\n",ans1,ans2);
}
return 0;
}