题目:https://codeforces.com/problemset/problem/1555/B
题意:给W,H为空间,x1,y1;x2,y2 为原有桌子的位置,w,h为新的桌子,求,最少移动多少距离,可以容纳下两桌子。
题解:
先获取最大的角落,和全部的缝隙
int maxh = max(y1, H - y2);
int minh = min(y1, H - y2);
int maxw = max(x1, W - x2);
int minw = min(x1, W - x2);
空隙 maxh+minh 和 maxw+minw
分成四种情款
(一)w,h空隙都不够
(二)w空隙不够,h空隙够用,要h腾出空间,使得w可使用空间为W;
(三)w空隙够,h空隙不够用,要w腾出空间,使得h可使用空间为H;
(二)w空隙够,h空隙够,比较上下或者左右的位移
AC:
//#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long int ll;
int main()
{
int t;
cin >> t;
while (t--)
{
int W, H, x1, y1, x2, y2, w, h;
cin >> W >> H;
cin >> x1 >> y1 >> x2 >> y2;
cin >> w >> h;
int maxh = max(y1, H - y2);
int minh = min(y1, H - y2);
int maxw = max(x1, W - x2);
int minw = min(x1, W - x2);
//cout << maxh << " " << minh << " " << maxw << " " << minw << endl;
double ans;
double x, y;
if (maxh + minh < h && maxw + minw < w)
{
cout << "-1" << endl;
}
else if (maxw + minw < w && maxh + minh >= h)//w不用移动
{
if (maxh >= h)
{
ans = 0;
}
else
{
ans = h - maxh;
}
printf("%.9lf\n", ans);
}
else if (maxw + minw >= w && maxh + minh < h)//h不用移动
{
if (maxw >= w)
{
ans = 0;
}
else
{
ans = w - maxw;
}
printf("%.9lf\n", ans);
}
else if (maxh + minh >= h && maxw + minw >= w)
{
double ans1, ans2;
if (maxh >= h)
{
ans1 = 0;
}
else
{
ans1 = h - maxh;
}
if (maxw >= w)
{
ans2 = 0;
}
else
{
ans2 = w - maxw;
}
ans = min(ans1, ans2);
printf("%.9lf\n", ans);
}
}
}
|