Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 104 Accepted Submission(s): 50
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int a[10005], b[10005], n;
typedef long long ll;
double sum;
bool check(double x) {
double now = 0;
for(int i = 1; i <= n; ++i)
if(a[i] >= x)
now += ((b[i] * b[i] * 1.0) / (a[i] * a[i])) * (a[i] - x) * (a[i] - x) * (a[i] - x);
if(now < sum) return true;
else return false;
}
int main()
{
int _;
scanf("%d", &_);
while(_ --)
{
int H = 0;
scanf("%d", &n);
for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); if(a[i] > H) H = a[i]; }
for(int i = 1; i <= n; ++i) scanf("%d", &b[i]);
sum = 0;
for(int i = 1; i <= n; ++i) sum += b[i] * b[i] * a[i];
sum /= 2;
double low = 0, high = H;
while((int)low != (int)high)
{
double h = (low + high) / 2;
if(check(h)) high = h;
else low = h;
}
printf("%d\n", (int)low);
}
return 0;
}
二分一个高度h, 因为只需求整数部分,当(int)low == (int)high时,二分结束
原文:http://www.cnblogs.com/orchidzjl/p/4803697.html