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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
智慧校园建设是在国家政策推动下,为深化教育改革、提升教育质量和管理效率而提出的重要项目。该项目旨在通过信息化手段,解决传统教育中存在的资源分散、管理混乱等问题,实现教育资源的高效利用和教学质量的全面提升。 目前,教育信息化虽取得一定进展,但面临“孤岛架构”的挑战,包括硬件资源无法共享、数据孤岛、应用孤岛等问题,导致资源浪费和管理效率低下。为此,智慧校园的建设目标聚焦于家校沟通便捷化、校园管理科学化、校园生活轻松化、课堂教学互动化和校园设施智能化,以提高教学效率和学生学习体验。 智慧校园的核心价值在于构建先进的网络教学平台和管理信息系统,实现教学资源的高效配置和利用,促进师生互动,提高管理效率,降低成本,构建健康高雅的生活环境。解决方案涵盖综合应用平台规划、系统架构设计、媒体发布、数字会议系统等,通过后台服务层、基础接入层和用户接入层的有机结合,实现智慧校园的全面功能。 智慧校园管理平台作为核心组成部分,提供模块化体系,包括公开课、直播、教学资源等23大应用,支持与第三方接口对接,实现多级管理。电教预约管理平台通过移动端APP或web后台简化预约流程,提高教室和会议室资源利用率,支持会议预订、审批、信息发布和环境管控。 教育录播系统和云平台支持教师制作和分享优质教学资源,进行在线组卷和评卷,同时提供学生应用,如高清视频录制、在线直播和互动交流,促进教学资源的共享和教育均衡化发展。这些系统的整合应用,将极大地推动教育信息化进程,实现教育资源的最大化利用和教育质量的全面提升。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值