Educational Codeforces Round 112 (Rated for Div. 2) B. Two Tables

Posted li_wen_zhuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 112 (Rated for Div. 2) B. Two Tables相关的知识,希望对你有一定的参考价值。

题目描述

You have an axis-aligned rectangle room with width W and height H, so the lower left corner is in point (0,0) and the upper right corner is in (W,H).
There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in (x1,y1), and the upper right corner in (x2,y2).
You want to place another rectangular table in this room with width w and height h with the width of the table parallel to the width of the room.
The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though).
You can’t rotate any of the tables, but you can move the first table inside the room.

What is the minimum distance you should move the first table to free enough space for the second one?

Input

The first line contains the single integer t (1≤t≤5000) — the number of the test cases.
The first line of each test case contains two integers W and H (1≤W,H≤108) — the width and the height of the room.
The second line contains four integers x1, y1, x2 and y2 (0≤x1<x2≤W; 0≤y1<y2≤H) — the coordinates of the corners of the first table.
The third line contains two integers w and h (1≤w≤W; 1≤h≤H) — the width and the height of the second table.

Output

For each test case, print the minimum distance you should move the first table, or −1 if there is no way to free enough space for the second table.
Your answer will be considered correct if its absolute or relative error doesn’t exceed 10−6.

Example

input
5
8 5
2 1 7 4
4 2
5 4
2 2 5 4
3 3
1 8
0 3 1 6
1 5
8 1
3 0 6 1
5 1
8 10
4 5 7 8
8 5
output
1.000000000
-1
2.000000000
2.000000000
0.000000000

Note

The configuration of the first test case is shown in the picture. But the movement of the first table is not optimal. One of the optimal movement, for example, is to move the table by (0,−1), so the lower left corner will move from (2,1) to (2,0). Then you can place the second table at (0,3)−(4,5).
In the second test case, there is no way to fit both tables in the room without intersecting.
In the third test case, you can move the first table by (0,2), so the lower left corner will move from (0,3) to (0,5).

题目大意

有一个房间,长为W,宽为H。房间中有一个桌子,左下角的坐标为(x1,y1),右上角的坐标为(x2,y2)。如果再在房间中放一个长度为w,宽为h的桌子,则至少要移动桌子的距离为多少(两桌子可以接触,但不能重叠)。

题目分析

首先我们先来进行一下分析:怎样才能将两张桌子放在房间中。
我们有有四种放法:
1)放在 长为W,宽为y1的长方形空间内。
2)放在 长为W,宽为H-y2的长方形空间内。
3)放在 长为x1,宽为H的长方形空间内。
4)放在 长为W-x2,宽为H的长方形空间内。

我们可以发现:每种方法都有一面为房间的长度,这一面不需要我们考虑。也就是说,在移动桌子时,我们至多只需要往一个方向移动即可,不需要在两个方向上移动

找出规律后这道题基本就做出来了:

  1. 判断是否有解:如果两桌子的长和宽之和都大于房间的长和宽,那么则无解。
  2. 找出长和宽空间的最大值:设长的最大值为 wx=max(x1,W-x2),宽的最大值为 hx=max(y1,H-y2)
    如 果 w x > = w 或 者 h x > = h , 则 不 需 要 进 行 移 动 , 输 出 0 如果 wx>=w 或者 hx>=h,则不需要进行移动,输出0 wx>=whx>=h0
  3. 分别计算出左右移动 和 上下移动的所需要的距离,取最小值即为最后的答案。
代码如下
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <algorithm>
#include <stack>
#include <queue>
#include <bitset>
#define LL long long
#define ULL unsigned long long
#define PII pair<LL,LL>
#define PDD pair<double,double>
#define x first
#define y second
using namespace std;
const int N=2e5+5;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int W,H,w,h;
		int x1,x2,y1,y2;
		scanf("%d%d",&W,&H);
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		scanf("%d%d",&w,&h);
		if(w+(x2-x1)>W&&h+(y2-y1)>H)		//判断是否有解
		{
			puts("-1");
			continue;
		}
		if(x1>=w||y1>=h||(W-x2)>=w||(H-y2)>=h) puts("0.000000000");
		else {
			int wx=max(x1,W-x2);		//找出左右方向上空间的最大值
			int hx=max(y1,H-y2);		//找出上下方向上空间的最大值
			double a=1e9,b=1e9;
			if(w+(x2-x1)<=W&&w>wx) a=w-wx;	//算出两方向上移动的距离
			if(h+(y2-y1)<=H&&h>hx) b=h-hx;
			printf("%.9lf\\n",min(a,b));
		}
	}
 	return 0;
}

以上是关于Educational Codeforces Round 112 (Rated for Div. 2) B. Two Tables的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27