Uva 10074【递推dp】
题意:求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】的更多相关文章
- 递推DP UVA 607 Scheduling Lectures
题目传送门 题意:教授给学生上课,有n个主题,每个主题有ti时间,上课有两个限制:1. 每个主题只能在一节课内讲完,不能分开在多节课:2. 必须按主题顺序讲,不能打乱.一节课L时间,如果提前下课了,按 ...
- 递推DP URAL 1167 Bicolored Horses
题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...
- 递推DP URAL 1017 Staircases
题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...
- 递推DP URAL 1260 Nudnik Photographer
题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- 递推DP URAL 1119 Metro
题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...
- 递推DP 赛码 1005 Game
题目传送门 /* 递推DP:官方题解 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩 ...
- 递推DP HDOJ 5328 Problem Killer
题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...
- hdu1978(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1978 分析: 递推DP. dp[][]表示可以到达改点的方法数. 刚开始:外循环扫描所有点dp[x][ ...
- 递推DP URAL 1031 Railway Tickets
题目传送门 /* 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 注意:s1与s2大小不一定,坑! 详细解释:http://blog.csdn.net/kk303/article/d ...
随机推荐
- 用javascript实现简单的用户登录验证
用javascript实现简单的用户登录验证 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- 全栈数据工程师养成攻略:Python 基本语法
全栈数据工程师养成攻略:Python 基本语法 Python简单易学,但又博大精深.许多人号称精通Python,却不会写Pythonic的代码,对很多常用包的使用也并不熟悉.学海无涯,我们先来了解一些 ...
- 计蒜客 Prefix Free Code(字典树+树状数组)
Consider n initial strings of lower case letters, where no initial string is a prefix of any other i ...
- scala的插值器
Scala 为我们提供了三种字符串插值的方式,分别是 s, f 和 raw.它们都是定义在 StringContext 中的方法. s 字符串插值器 val a = 2println(s"小 ...
- 五.反馈(Hopfield)神经网络
前馈网络一般指前馈神经网络或前馈型神经网络.它是一种最简单的神经网络,各神经元分层排列.每个神经元只与前一层的神经元相连.接收前一层的输出,并输出给下一层,数据正想流动,输出仅由当前的输入和网络权值决 ...
- 如何把pdf文档转化为word
在工作中常常遇到大量的pdf文档,再加工进行处理文件,特别的不方便,需要转换为WORD. 尝试如下: 使用wps自带的工具转换,提示需要是会员才能进行.否则只能进行5页以下的转换. 再想是不是又有个这 ...
- Win32 Console Application、Win32 Application、MFC三者之间的联系和区别
转自:http://blog.csdn.net/c_base_jin/article/details/52304845 在windows编程中,我们或多或少都听说这三个名称,分别是Win32 Cons ...
- 苹果审核不通过,程序/游戏不兼容IPV6网络
最近苹果升级的IOS10,所以那边网络环境变成IPV6,如果你的程序不兼容IPV6,苹果的程序会以这个不兼容的原因驳回审核. 那么如何让自己的程序兼容这个?方法其实C#本来已经提供给你的,而且很简单, ...
- OpenCV灰度化图像
OpenCV2版本号非常多函数发生了变化.比如二值化,其演示样例: void CmyMFC2Dlg::OnBnClickedButton1() { // TODO: Add your control ...
- [Vue CLI 3] Uglify 相关的应用和设计
在本文开始之前,先留一个问题? 如果在新版本我想加一个 drop_console 的配置呢? 在老版本的脚手架生成的配置中,对于线上环境的文件:webpack.prod.conf.js 使用了插件:u ...