CF303B:Rectangle Puzzle II(数学)

B. Rectangle Puzzle II
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rectangle grid. That grid's size is n × m. Let's denote the coordinate system on the grid. So, each point on the grid will have coordinates — a pair of integers (x, y) (0 ≤ x ≤ n, 0 ≤ y ≤ m).

Your task is to find a maximum sub-rectangle on the grid (x1, y1, x2, y2) so that it contains the given point (x, y), and its length-width ratio is exactly (a, b). In other words the following conditions must hold: 0 ≤ x1 ≤ x ≤ x2 ≤ n0 ≤ y1 ≤ y ≤ y2 ≤ m.

The sides of this sub-rectangle should be parallel to the axes. And values x1, y1, x2, y2 should be integers.

If there are multiple solutions, find the rectangle which is closest to (x, y). Here "closest" means the Euclid distance between (x, y) and the center of the rectangle is as small as possible. If there are still multiple solutions, find the lexicographically minimum one. Here "lexicographically minimum" means that we should consider the sub-rectangle as sequence of integers (x1, y1, x2, y2), so we can choose the lexicographically minimum one.

Input

The first line contains six integers n, m, x, y, a, b (1 ≤ n, m ≤ 109, 0 ≤ x ≤ n, 0 ≤ y ≤ m, 1 ≤ a ≤ n, 1 ≤ b ≤ m).

Output

Print four integers x1, y1, x2, y2, which represent the founded sub-rectangle whose left-bottom point is (x1, y1) and right-up point is (x2, y2).

Examples
input
9 9 5 5 2 1
output
1 3 9 7
input
100 100 52 50 46 56
output
17 8 86 92
题意:给一个N*M的矩阵,要求在里面找一个最大的长宽比为a:b的矩形,且该矩形需要包含(x,y)点,且该矩形中心点离(x,y)欧几里得距离最近,输出左下角和右上角的坐标,若有多个答案,输出字典序最小那个。

思路:先确定矩形最大能取多大,然后算一下以xy为中心的横纵坐标,上下取整注意下就行。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    LL n, m, x, y, a, b;
    scanf("%lld%lld%lld%lld%lld%lld",&n,&m,&x,&y,&a,&b);
    LL g = gcd(a,b);
    a /= g, b /= g;
    LL t = min(n/a,m/b);
    a = a*t, b = b*t;
    LL down = x-(a+1)/2;
    if(down < 0)
        down = 0;
    else if(down + a > n) down -= down+a-n;
    LL up = y-(b+1)/2;
    if(up < 0) up = 0;
    else if(up + b > m) up -= up+b-m;
    printf("%lld %lld %lld %lld\n",down, up, down+a, up+b);
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值