UVa 10074

题意:求01矩阵的最大子0矩阵。

http://www.csie.ntnu.edu.tw/~u91029/MaximumSubarray.html#2

这里说的很清楚。先求Largest Empty Interval,枚举每个点为矩形的右下角。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = ;
int Map[MAXN][MAXN], width[MAXN][MAXN];
int row, col; int main()
{
while (cin>>row>>col&&!(row==&&col==))
{
int ans = ;
for(int i=;i<=row;i++)
for (int j = ; j <= col; j++) {
cin >> Map[i][j];
if (Map[i][j]) width[i][j] = ;
else width[i][j] = width[i][j - ] + ;
}
for (int i = ; i <= row; i++)
{
for (int j = ; j <= col; j++) {
int w = 1e9;
for (int h = ; i - h + > ; h++) {
if (Map[i][j]) break;
w = min(w, width[i - h + ][j]);
ans = max(ans, w*h);
}
}
}
cout << ans << endl;
}
return ;
}

按照下一个更高效的算法写,不知道为什么会WA,可能是哪里有问题。。。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = ;
int Map[MAXN][MAXN];
int wl[MAXN], wr[MAXN];
int h[MAXN], l[MAXN], r[MAXN];
int row, col; int main()
{
while (scanf("%d%d", &row, &col) == , !(row == && col == ))
{
int ans = ;
for (int i = ; i <= row; i++)
for (int j = ; j <= col; j++)
scanf("%d", &Map[i][j]);
for (int i = ; i <= row; i++)
{
for (int j = ; j <= col; j++)
if (Map[i][j]) wl[j] = ;
else wl[j] = wl[j - ] + ; for (int j = col; j >= ; j--)
if (Map[i][j]) wr[j] = ;
else wr[j] = wr[j + ] + ; for (int j = ; j <= row; j++)
if (Map[i][j]) h[j] = ;
else h[j] = h[j] + ; for (int j = ; j <= col; j++)
if (r[j] == ) r[j] = wr[j];
else r[j] = min(wr[j], r[j]); for (int j = ; j <= col; j++)
if (l[j] == ) l[j] = wl[j];
else l[j] = min(wl[j], l[j]); for (int j = ; j <= col; j++)
ans = max(ans, (l[j] + r[j] - )*h[j]);
}
printf("%d\n", ans);
}
return ;
}

WA1

最高效的按照栈那种方式写也是WA。。。。。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
const int MAXN = ;
int a[MAXN][MAXN], h[MAXN][MAXN];
int col, row; int main()
{
while (cin >> row >> col)
{
if (!col && !row) break;
for (int i = ; i <= row; i++)
for (int j = ; j <= col; j++)
cin >> a[i][j];
memset(h, , sizeof(h));
for (int i = ; i <= row; i++)
for (int j = ; j <= col; j++)
if (a[i][j]) h[i][j] = ;
else h[i][j] = h[i - ][j] + ; stack<int> st;
int area;
int mx = ;
for (int i = ; i <= row; i++)
{
int j;
for (j = ; j <= col;) {
if (st.empty() || h[i][st.top()] <= h[i][j])
st.push(j++);
else {
int top = st.top();
st.pop();
if (st.empty())
area = h[i][top] * j;
else
area = h[i][top] * (j - st.top() - );
mx = max(mx, area);
}
}
while (!st.empty())
{
int top = st.top();
st.pop();
if (st.empty()) area = h[i][top] * j;
else
area = h[i][top] * (j - st.top() - );
mx = max(mx, area);
}
}
cout << mx << endl;
}
return ;
}

WA2

Uva 10074【递推dp】的更多相关文章

  1. 递推DP UVA 607 Scheduling Lectures

    题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...

  2. 递推DP URAL 1167 Bicolored Horses

    题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...

  3. 递推DP URAL 1017 Staircases

    题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...

  4. 递推DP URAL 1260 Nudnik Photographer

    题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...

  5. 递推DP URAL 1353 Milliard Vasya's Function

    题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...

  6. 递推DP URAL 1119 Metro

    题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...

  7. 递推DP 赛码 1005 Game

    题目传送门 /* 递推DP:官方题解 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩 ...

  8. 递推DP HDOJ 5328 Problem Killer

    题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...

  9. hdu1978(递推dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1978 分析: 递推DP. dp[][]表示可以到达改点的方法数. 刚开始:外循环扫描所有点dp[x][ ...

  10. 递推DP URAL 1031 Railway Tickets

    题目传送门 /* 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 注意:s1与s2大小不一定,坑! 详细解释:http://blog.csdn.net/kk303/article/d ...

随机推荐

  1. 在rabbitmq操作页面上添加队列、交换器及绑定示图

    1.添加队列 2.添加交换器 3.绑定

  2. Kotlin 委托(1)类委托、变量委托注意事项

    1.官方文档 英文: https://kotlinlang.org/docs/reference/delegation.html https://kotlinlang.org/docs/referen ...

  3. Hackerrank--Prime Sum

    题目链接 The problem is quite simple. You're given a number N and a positive integer K. Tell if N can be ...

  4. 根据网站运行日志猜测的百度蜘蛛ip

    da大部分文章都是吵来吵去,不准确 所以就不参考那些沙雕的文章了,直接自己统计一个 123.125.71.117 123.125.71.58 220.181.108.115 220.181.108.1 ...

  5. day36 07-Hibernate抓取策略:many-to-one上的抓取策略

    package cn.itcast.test; import java.util.List; import org.hibernate.Hibernate; import org.hibernate. ...

  6. GDOI2017第四轮day1总结

    总的来说这场比赛,只能说是勉强正常发挥. 实在是知识水平有限,最后没能突破瓶颈. 有几个做得好的地方: 1.想好了在写题: 2.暴力也会拍 3.适当地放弃题. 要学习的东西: 1.Sg,线性基: 2. ...

  7. Python中输入和输出(打印)数据

    一个程序要进行交互,就需要进行输入,进行输入→处理→输出的过程.所以就需要用到输入和输出功能.同样的,在Python中,怎么实现输入和输出? Python3中的输入方式: Python提供了 inpu ...

  8. [Vue CLI 3] 配置解析之 parallel

    官方文档中介绍过在 vue.config.js 文件中可以配置 parallel,作用如下: 是否为 Babel 或 TypeScript 使用 thread-loader. 该选项在系统的 CPU ...

  9. vue组件通信全面总结

    写在前面 组件间的通信是是实际开发中非常常用的一环,如何使用对项目整体设计.开发.规范都有很实际的的作用,我在项目开发中对此深有体会,总结下vue组件间通信的几种方式,讨论下各自的使用场景 文章对相关 ...

  10. laravel后台扩展包

    https://github.com/the-control-group/voyager