CF 549H. 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||.

Input

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

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.

Sample test(s)
input
1 2
3 4
output
0.2000000000
input
1 0
0 1
output
0.5000000000
Note

In the first sample matrix B is 

In the second sample matrix B is 

从头到尾都理解错了题目意思。。

题意:对A矩阵每个值做更改,使得得到的矩阵B为退化矩阵(ad-bc=0),求四个更改的差值里的绝对值最大的差值的最小值。

我一直以为当四个差值绝对值相等的情况下才最优。。。

枚举这个差值的最大值,二分,这里不用while(l<=r),因为是浮点数。我们就直接设定判断100次。

求出两个最边界值,如果最大值>=0&&最小值<=0,那么一定会存在某种情况使ad-bc=0,从而满足B是退化矩阵。但要注意不能先入为主地断定(a+mid)*(d+mid)是最大值,(a-mid)*(d-mid)是最小值。

#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<queue>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll long long
using namespace std;
int main(){
    double a,b,c,d;
    cin>>a>>b>>c>>d;
    double l=0,r=1000000000,mid;
    for(int i=0;i<100;++i){
        mid=(l+r)/2;
        double max_x=max(max((a+mid)*(d+mid),(a-mid)*(d+mid)),max((a+mid)*(d-mid),(a-mid)*(d-mid)));
        double min_x=min(min((a+mid)*(d+mid),(a-mid)*(d+mid)),min((a+mid)*(d-mid),(a-mid)*(d-mid)));
        double max_y=max(max((b+mid)*(c+mid),(b-mid)*(c+mid)),max((b+mid)*(c-mid),(b-mid)*(c-mid)));
        double min_y=min(min((b+mid)*(c+mid),(b-mid)*(c+mid)),min((b+mid)*(c-mid),(b-mid)*(c-mid)));
        if(max_x-min_y>=0&&min_x-max_y<=0)
            r=mid;
        else
            l=mid;
    } 
    printf("%.10lf\n",l);
    return 0;
}

你可能感兴趣的:(CF 549H. Degenerate Matrix(二分,退化矩阵灵活题))