Codeforce 22B Bargaining Table
来源:互联网 发布:stewart mac 编辑:程序博客网 时间:2024/06/05 14:48
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n × m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
The first line contains 2 space-separated numbers n and m (1 ≤ n, m ≤ 25) — the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
Output one number — the maximum possible perimeter of a bargaining table for Bob's office room.
3 3000010000
8
5 411000000000000000000
16
HDU 1505,1506的变形 只是由求面积变成了求周长 具体分析可见http://blog.csdn.net/iooden/article/details/38379065
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 30;int l[N][N], r[N][N], h[N][N], n, m, ans;char a[N][N];int main(){ scanf ("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { h[i][0] = h[i][m + 1] = -1; scanf ("%s", a[i] + 1); for (int j = 1; j <= m; ++j) { if (a[i][j] == '0') h[i][j] = h[i - 1][j] + 1; l[i][j] = r[i][j] = j; } } int ans = 0; for (int i = 1; i <= n; ++i) { for (int j = m; j >= 1; --j) while (h[i][r[i][j] + 1] >= h[i][j]&&h[i][j]) r[i][j] = r[i][r[i][j] + 1]; for (int j = 1; j <= m; ++j) { while (h[i][l[i][j] - 1] >= h[i][j]&&h[i][j]) l[i][j] = l[i][l[i][j] - 1]; ans = max (ans, r[i][j] - l[i][j] + 1 + h[i][j]); } } printf ("%d\n", 2 * ans); return 0;}
另外这题数据比较小 也可以暴力枚举 枚举每点作为左上角 然后枚举合法的的长和宽, 判断形成的的矩阵是否全由 '0'组成, 如果是就更新结果
#include<cstdio>#include<cstring>const int maxn = 30;int n, m;int a[maxn][maxn];int sum[maxn][maxn];int main(){ while (~scanf ("%d%d", &n, &m)) { memset (a, 0, sizeof (a)); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf ("%1d", &a[i][j]); memset (sum, 0, sizeof (sum)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + a[i][j]; } } int x, y; int ans = 0; for (int x1 = 1; x1 <= n; x1++) for (int y1 = 1; y1 <= m; y1++) for (int x2 = x1; x2 <= n; x2++) for (int y2 = y1; y2 <= m; y2++) { int tmp = sum[x2][y2] + sum[x1 - 1][y1 - 1] - sum[x1 - 1][y2] - sum[x2][y1 - 1]; if (tmp == 0) { int dx = x2 - x1 + 1; int dy = y2 - y1 + 1; if (ans < (dx + dy) * 2) ans = (dx + dy) * 2; } } printf ("%d\n", ans); } return 0;}
- Codeforce 22B Bargaining Table
- Codeforce 22B Bargaining Table
- Codeforces 22B Bargaining Table
- codeforces 22B B. Bargaining Table(dp)
- codeforces 22B Bargaining Table dp
- CodeForces 22B Bargaining Table (简单题)
- Code Forces 22B Bargaining Table
- Codeforces 22B Bargaining Table【暴力+思维】
- Codeforces Beta Round #22 (Div. 2 Only)-B. Bargaining Table
- Bargaining Table
- CodeForces 22B Bargaining Table 01矩阵求最大矩形面积
- cf#22-B-Bargaining Table-枚举+贪心+dp(最大连续区间和)
- 【打CF,学算法——二星级】Codeforces 22B Bargaining Table(区域和)
- CodeForce Round Table Knights
- Codeforce # B Sequence Formatting
- codeforce 298 B Sail
- 【codeforce】B. Flag Day
- codeforce B. Road Construction
- hdu 4915 Parenthese sequence(高效)
- 重装Ubuntu 保留/home分区中的数据
- java中内部类的创建四种情况,三种方式,及内部数据访问权限
- 深拷贝 和 浅拷贝
- 【java】Integer == Integer?
- Codeforce 22B Bargaining Table
- log4cplus 日志应用
- c++学习——第一天
- 中断控制器及中断控制
- CSDN见证了我的成长
- 與 Roy Osherove 探討單元測試的藝術 (心得筆記)
- MYSQL 常用命令
- hdu 4883 TIANKENG’s restaurant
- UVa 1584 Circular Sequence