HDU 5301 Buildings(思维:一道简单的难题)

来源:互联网 发布:恺英网络市值 编辑:程序博客网 时间:2024/05/21 14:00
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 23 3 1 1
 

Sample Output
12
Hint
Case 1 :
You can split the floor into five $1 \times 1$ apartments. The answer is 1.Case 2:
You can split the floor into three $2 \times 1$ apartments and two $1\times 1$ apartments. The answer is 2.
If you want to split the floor into eight $1 \times 1$ apartments, it will be unacceptable because the apartment located on (2,2) can't have windows.
 

Author
XJZX
 

Source
2015 Multi-University Training Contest 2
 
看着简单的一道题,一直WA,一直不肯放弃,于是5个小时有4个小时都在WA这道题。。。。

题意:
给出大矩形n*m,另给出一个坐标x,y
要求划分矩形成若干个小矩形,其中每个小矩形都至少有一条边贴着大矩形
而给出的x,y的格子不能被划分,求划分的矩形的所有方案中的最大矩形中的最小的矩形的面积
分析:
首先,看看测试组数。。。wc、、、10000组。。。
其次,看看别人的代码长度。。。wc、、、300B 这么短!!!这是敲的Hello World????
然后开始思考,如果没有(x,y)那个不能划分的部分,这就一Hello World级别的题目
直接输出min((n+1)/2,(m+1)/2)即为正解
但是有一个不能划分的(x,y)...当时感觉还是很简单,但是整整写了9遍,WA了4个小时。。。
现在考虑几组数据:
3 3 2 2
5 5 3 3
5 5 1 1
99 100 2 2
6 99 1 45
我发现,当矩形是一个正方形的时候,(x,y)不管在哪里都不会影响结果,除了正方形正中心
if (n == m) ans = (n+1)/2;
if(x,y)在正方形正中心 ans--;
然后,我们只分析n<m这一种情况
如果没有(x,y)这个点,答案将是ans = (n+1)/2;//因为设定n<m,我们划分竖着的矩形
但是有x就会造成不同,就99 100 2 2这个例子,答案是50((n+1)/2)
以(x,y)这个点所在的列为分界线将矩形划分为左右两边
距离这一列较近的边界是左边,距离是2
那么只需要右边的还是按没有(x,y)这个点的情况划分竖着的矩形
左边则划分横着的矩形,而横着的矩形的大小是小于ans的故这种情况答案就是ans
对于6 99 1 45这个例子呢
还是以(x,y)这个点所在的列为分界线将矩形划分为左右两边
但是此时即使是距离这一列较近的边都比ans要大,
这个例子的答案是5,就是全部划分成竖着的矩形,除了(x,y)所在列矩形是5,其他都是3
这个例子里没有横着的矩形,横着的矩形远远大于竖着的5,不划算
所以当距离(x,y)所在列最近的边大于ans的时候,就要判断是选择还是用竖着的矩形还是像99 100 2 2 那样使用横着的矩形
于是在int h = max(x-1,n-x);  int w = min(y-1,m-y)+1;这两种方案中取最优方案ans = ans = max(ans,min(h,w));
思维题,代码不长,一个情况没考虑清楚就会WA好久。。。
#include<iostream>#include<cstdio>using namespace std;int main(){    int n,m,x,y;    while (~scanf("%d %d %d %d",&n,&m,&x,&y))    {        if (n == m)        {            int ans = (n+1)/2;            if (2*x-1==n&&2*y-1==m) ans--;            printf("%d\n",ans);            continue;        }        if (n > m)        {            swap(n,m);            swap(x,y);        }        int ans = (n+1)/2;        int h = max(x-1,n-x);        int w = min(y-1,m-y)+1;        if (w>ans) ans = max(ans,min(h,w));        printf("%d\n",ans);    }    return 0;}


0 0