Because of recent update of ZOJ, some special judges were broken. You're requested to fix the special judge for Problem 9999.
In Problem 9999, there is only one case in where the standard output is two real number x and y. The special judge uses two parameters: one is the sum of the two numbers in standard output (x and y); the other is the sum of the two numbers in the output of submitted solution (p and q). If the absolute value of the difference of these two parameters does not exceed k, the submitted solution will be accepted.
Knowing that the range of p is [0, a] and the range of q is [0, b]. What's the possibility that a solution outputting p and q with equal probability will get accepted.
Input
There are no more than 10000 test cases. You should process to the end of input. For each test case, there are only one line with 5 real numbers x, y, a, b, k, (0 <= x <= 1000, 0 <= y <= 1000, 0 <= a <= 1000, 0 <= b <= 1000, 0 <= k <= 1000).
Output
For each case, you should print out a single line indicating the possibility of getting accepted using the random solution. Your answer is supposed to be rounded to six decimal digits.
Sample Input
2 2 4 4 4
2 2 2 2 1
Sample Output
1.000000
0.125000
题意:
当标准答案和你提交的解的误差不超过 K 的时候,你的答案将被认为是正确的.
思路:
分情况讨论然后模拟一下即可.
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const double esp = 1e-8;
double x, y,a, b, k;
int dcmp(double x){
if(fabs(x) < esp)
return 0;
if (x < 0)
return -1;
return 1;
}
double solve(double x){
double ans = 0;
if (x < a)
ans -= x * x / 2;
else if (x >= a && x <= b){
ans -= a * a / 2;
ans -= a * (x - a);
}else {
ans -= a * a / 2;
ans -= a * (b - a);
ans -= (x - b) * (a - (x - b) +a) / 2;
}return ans;
}
int main(){
while(scanf("%lf %lf %lf %lf %lf", &x, &y, &a, &b, &k) != EOF){
double sum = x + y;
if (dcmp(a) == 0 && dcmp(b) == 0)
if(dcmp(sum - k) <= 0 && dcmp(sum + k ) >= 0)
printf("1.000000\n");
else printf("0.000000\n");
else if (sum + k < 0 || sum - k > a + b)
printf("0.000000\n");
else if (dcmp(a) == 0){
double minx = max(0.0, sum - k);
double maxx = min(b, sum + k);
printf("%0.6f\n",(maxx - minx) / b);
} else if (dcmp(b) == 0){
double minx = max(0.0, sum - k);
double maxx = min(a, sum + k);
printf("%0.6f\n",(maxx - minx) / a);
} else {
double sum1 = 0;
if (a > b)
swap(a, b);
double yx = max(sum - k, 0.0);
double yy = min(sum + k, a + b);
sum1 += solve(yx);
sum1 += solve(a + b - yy);
printf("%.6f\n",(a * b + sum1)/ ( a * b));
}
}return 0;
}