[关闭]
@tenlee 2015-07-25T09:26:19.000000Z 字数 1327 阅读 1572

HDU 5301 Buildings(2015多校联合)

HDUOJ 题解


题目链接:戳我

题目大意:

nm列的矩阵,删除一个格子x,y。用矩形来填充矩阵。且矩形至少有一边是在矩阵的边缘上。求满足条件的矩形填充方式中面积最大的矩形,要使得该最大矩形的面积最小。

样例分析:

见题

解题思路:

任何矩形都可以分为宽度为1的小矩形,所以只考虑矩形的可以的最小长度即可。

讨论:
m 代表行,n 代表列
输入的时候先输入行,在输入列,当行比列大的时候要交换
任何矩形都可以分为宽度为1的小矩形,所以只考虑矩形的可以的最小长度即可。宽度始终为 1
当没有删除格子时,最小的面积是 ans=min((n+1)/2,(m+1)/2)
当 x,y处于矩形正中央时 ans- 1即可
其他情况:

如果m > n swap(n,m), swap(x,y)
之后如图所示,上下取大的 e,左右取小的f
面积就应该为 ans2 = min(e, f)
如果这个 ans2 比 最小面积ans小,肯定输入ans
否则就是 ans2

此处输入图片的描述

代码:

  1. //Author LJH
  2. //www.cnblogs.com/tenlee
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cctype>
  7. #include <cmath>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <queue>
  11. #include <stack>
  12. #include <map>
  13. #define clc(a, b) memset(a, b, sizeof(a))
  14. using namespace std;
  15. const int inf = 0x3f;
  16. const int INF = 0x3f3f3f3f;
  17. const int maxn = 1e6+5;
  18. inline int mymin(int x, int y)
  19. {
  20. return x < y ? x : y;
  21. }
  22. inline int mymax(int x, int y)
  23. {
  24. return x > y ? x : y;
  25. }
  26. int main()
  27. {
  28. //freopen("Mul2/data/data1002/b.in", "r", stdin);
  29. //freopen("2.out", "w", stdout);
  30. int n, m, x, y;
  31. int le, ri, up, down;
  32. int e, f, g;
  33. int ans = 0;
  34. while(~scanf("%d %d %d %d", &m, &n, &x, &y))
  35. {
  36. if(m > n)//n为长边,m为短边
  37. {
  38. swap(n, m); swap(x, y);
  39. }
  40. le = y - 1;//删除的格子距左端的距离
  41. ri = n - y; //删除的格子距右端的距离
  42. e = mymin(le, ri);
  43. up = x - 1; //删除的格子距上端的距离
  44. down = m - x;//删除的格子距下端的距离
  45. f = mymax(up, down);
  46. g = mymin(e+1, f);
  47. ans = (m+1) / 2;//没有删除格子时,最小的面积
  48. if(le == ri && ri == up && up== down)//如果n,m为奇数,且删除的格子在正中间
  49. {
  50. printf("%d\n", le);
  51. }
  52. else
  53. {
  54. printf("%d\n", mymax(ans, mymin(e+1, f)));
  55. }
  56. }
  57. return 0;
  58. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注