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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值