HDU 5301 Buildings

来源:互联网 发布:wow强者的旗帜 淘宝 编辑:程序博客网 时间:2024/06/05 23:42

分析

你当前的任务是要为将于HZXJHS建造的住宅楼做一份地面规划。所以你需要找到一个合理的方式划分地面以建造矩形的楼墙。每一面墙都应该和建筑物的边平行。

这块地皮可以视为一个长宽n×m的矩形,在这块地皮上规划一每栋占地占地a×b的楼群。对于每一栋楼,其占地面积可以与其他楼不同。但其长宽ab都应是整数。

另外,这些楼群必须完全覆盖这块地皮,除了一个预先规划的位于(x,y)的占地1×1广场square)。这些楼可以并拢,但不能和其他楼共占一块地面。

举一个栗子,当n=2,m=3,x=2,y=2时,如下图所示。

除此之外,考虑采光每一栋楼都应预留开窗的位置。因此,每一栋楼为了避免其采光被另外的楼遮挡,都应有一面墙是直接面向这个地皮的外围,才能开窗。

你的老板XXY希望你能在这块地皮上尽可能规划建造较小占地面积的楼(为了采光你可能需要扩大占地面积),给出该方案中面积最大的楼的占地面积。


简要地读懂题意后,思考这个类似于拼积木的问题。

首先是楼的大小,因为要求尽可能的占地小,所以其大小应该为1×b。例如2×2可以建造两栋1×2

再而是考虑如何规划,先忽视广场这个影响因素。因为其楼这个矩形至少一边必须与地皮这个矩形的一边重合。不妨先在外围用1×1,比如n=2,m=3时,恰好铺满。再比如n=3,m=3时,这时发现,(2,2)这一块地方是没有办法采光的(暂不考虑广场),那么应该将其与其他楼合并,用1×2的。

同理,n=4,m=4时,有四块需要和其他楼合并。这是可以发现,找哪一个楼和中间楼合并才是需要解决的问题。只要有一个能合并,那么旁边与之平行的矩形均可以铺过。

此时,引入广场这个影响因素,显然,我们只需要考虑这个广场到四条边的方向上的方块如何合并。自然我们会考虑取这四方向上最长的,即是我们要确定的b

但是如果存在这样的情况,当n=5,m=6,x=4,y=5时,最长的是4,但是4并不是正确答案,因为第4格可以由x方向的楼合并,此时其长度也仅为3。于是,有了这样一个大致思路。

为了确保讨论不重复,现确定下面讨论的方向,不妨定m为长,n为宽。

取y方向(与长平行)的两个长度的最小值p;再取x方向(与宽平行)的两个长度的最大值q;比较p+1和q的大小(即检查y方向是否能合并x方向的最后一个,以缩减一长度)得到最小的b值r。但是如果r的长度超过了该矩形的一半显然是不合理的,可以由对称的思想对分这个地皮也能得解,比较r和地皮该方向的边长的一半得到解。

但是,这样的思路依然存在问题,如果这个矩形是正方形且广场建在中央呢?因为上述的讨论的最大值是边长的一半,但是这里如果是在中央,那么将小于边长的一半,所以还需要一个特判。

思路

因为解法中存在较多取大取小的方法,这里定义了宏,来实现取大取小,但不建议这么做,好像也只是代码长度短一些。

代码

#include <cstdio>#define MAX(a, b) (a)>(b)?(a):(b)#define MIN(a, b) (a)<(b)?(a):(b)int n, m, x, y;int solve(){    if (n > m) { int t = n; n = m; m = t; t = x; x = y; y = t; }    if (n == m && x == y && n == (x<<1)-1) return (x-1);    return MAX(MIN(MIN(y, m-y+1), MAX(x-1, n-x)), (n+1)>>1);}int main(){    while (~scanf("%d%d%d%d", &n, &m, &x, &y))        printf("%d\n", solve());    return 0;}

题目

Description

Your current task is to make a ground plan for a residential building located in HZXJHS. So you must determine a way to split the floor building with walls to make apartments in the shape of a rectangle. Each built wall must be paralled to the building’s sides.

The floor is represented in the ground plan as a large rectangle with dimensions n×m, where each apartment is a smaller rectangle with dimensions a×b located inside. For each apartment, its dimensions can be different from each other. The number a and b must be integers.

Additionally, the apartments must completely cover the floor without one 1×1 square located on (x,y). The apartments must not intersect, but they can touch.

For this example, this is a sample of n=2,m=3,x=2,y=2.

To prevent darkness indoors, the apartments must have windows. Therefore, each apartment must share its at least one side with the edge of the rectangle representing the floor so it is possible to place a window.

Your boss XXY wants to minimize the maximum areas of all apartments, now it’s your turn to tell him the answer.

Input

There are at most 10000 testcases.
For each testcase, only four space-separated integers, n,m,x,y(1n,m108,n×m>1,1xn,1ym).

Output

For each testcase, print only one interger, representing the answer.

Sample Input

2 3 2 2
3 3 1 1

Sample Output

1
2

0 0
原创粉丝点击