codeforces Looksery Cup 2015 H Degenerate Matrix
The determinant of a matrix 2 × 2 is defined as follows:
A matrix is called degenerate if its determinant is equal to zero.
The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements.
You are given a matrix . Consider any degenerate matrix B such that norm ||A - B|| is minimum possible. Determine||A - B||.
The first line contains two integers a and b (|a|, |b| ≤ 109), the elements of the first row of matrix A.
The second line contains two integers c and d (|c|, |d| ≤ 109) the elements of the second row of matrix A.
Output a single real number, the minimum possible value of ||A - B||. Your answer is considered to be correct if its absolute or relative error does not exceed 10 - 9.
1 2 3 4
0.2000000000
1 0 0 1
0.5000000000
In the first sample matrix B is
In the second sample matrix B is
这道题可以用二分做,因为要求矩阵最大值的最小值,所以最后A矩阵的每个元素和B矩阵的每个元素差值都小于等于一个数时最小,所以从0到10^9枚举差值,然后根据范围二分。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int n,m,i,j;
double l,r,mid,a,b,c,d,a1,a2,b1,b2,c1,c2,d1,d2,t1,t2,s1,s2;
while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF)
{
l=0.0,r=1000000000.0;
for(i=1;i<=100000;i++){
mid=(l+r)/2.0;
a1=a+mid;a2=a-mid;
b1=b+mid;b2=b-mid;
c1=c+mid;c2=c-mid;
d1=d+mid;d2=d-mid;
t1=min(min(a1*d1,a1*d2),min(a2*d1,a2*d2));
t2=min(min(b1*c1,b1*c2),min(b2*c1,b2*c2));
s1=max(max(a1*d1,a1*d2),max(a2*d1,a2*d2));
s2=max(max(b1*c1,b1*c2),max(b2*c1,b2*c2));
if(t1<=s2 && t2<=s1)r=mid;
else l=mid;
}
double ans=l;
printf("%.11f\n",ans);
}
return 0;
}