题目:Two Table
B. Two Tables
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You have an axis-aligned rectangle room with width 𝑊W and height 𝐻H, so the lower left corner is in point (0,0)(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 (𝑥1,𝑦1)(x1,y1), and the upper right corner in (𝑥2,𝑦2)(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.
Example of how you may move the first table.
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≤𝑡≤50001≤t≤5000) — the number of the test cases.
The first line of each test case contains two integers 𝑊W and 𝐻H (1≤𝑊,𝐻≤1081≤W,H≤108) — the width and the height of the room.
The second line contains four integers 𝑥1x1, 𝑦1y1, 𝑥2x2 and 𝑦2y2 (0≤𝑥1<𝑥2≤𝑊0≤x1<x2≤W; 0≤𝑦1<𝑦2≤𝐻0≤y1<y2≤H) — the coordinates of the corners of the first table.
The third line contains two integers 𝑤w and ℎh (1≤𝑤≤𝑊1≤w≤W; 1≤ℎ≤𝐻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−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−610−6.
题目意思:在给的长度宽度的一个限定的地方,里面已经又了一个已知长度宽度的长方形桌子,现在再放入一个新的桌子,问能不能放下,不能输出-1,能的话那么就输出需要移动的位置。(只能平移,不能旋转)
思路:再放入一个桌子 ,能不能放的进去就要看能在水平方向上移动的距离,以及在竖直方向上移动的距离。
1.能放的进去
1)只能水平移动放进去
2)只能竖直移动放进去
3)都能
2.不能放进去(直接让ans=-1可以偷懒省去这部分的判断)
·
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxn=100000;
int t;
int main()
{
cin>>t;
while (t--) {
int w,h,x1,x2,y1,y2,nx,ny,l,k,maxw,maxh,minw,minh,s1,s2,ans=-1;
cin>>w>>h>>x1>>y1>>x2>>y2>>nx>>ny;
l=x2-x1;
k=y2-y1;
maxw=max(x1,w-x2);//最多空的格子
maxh=max(y1, h-y2);
minw=min(x1, w-x2);
minh=min(y1, h-y2);
s1=maxw+minw;
s2=maxh+minh;
//桌子移动的距离就是nx-maxw或者ny-maxh
if (nx<=s1&&ny>s2) {//水平移动
if (maxw>=nx) {
ans=0;
}
else{
ans=nx-maxw;
}
}
else if(ny<=s2&&nx>s1){//竖直移动
if (maxh>ny) {
ans=0;
}
else{
ans=ny-maxh;
}
}
else if(s2>=ny&&s1>=nx){//既可以水平移动也可以竖直移动
int ans1,ans2;
if (maxh>=ny) {
ans1=0;
}
else{
ans1=ny-maxh;
}
if (maxw>=nx) {
ans2=0;
}
else{
ans2=nx-maxw;
}
ans=min(ans1,ans2);
}
cout<<ans<<endl;
}
}