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 ...
随机推荐
- Effective Modern C++ 条款1:理解模板型别推导
成百上千的程序员都在向函数模板传递实参,并拿到了完全满意的结果,而这些程序员中却有很多对这些函数使用的型别是如何被推导出的过程连最模糊的描述都讲不出来. 但是当模板型别推导规则应用于auto语境时,它 ...
- Odoo 中的widget
many2many_tags one2many_list selection progressbar selection statusbar handle monetary mail_thread s ...
- poj 2318 TOYS(计算几何 点与线段的关系)
TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12015 Accepted: 5792 Description ...
- 计蒜客 Flashing Fluorescents(状压DP)
You have nn lights, each with its own button, in a line. Pressing a light’s button will toggle that ...
- Leetcode63.Unique Paths II不同路径2
一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为"Start" ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为" ...
- C++中的 istringstream 的用法
C++引入了ostringstream.istringstream.stringstream这三个类,要使用他们创建对象就必须包含<sstream>这个头文件. istringstream ...
- python 中初始化二维数组的方法
最好的方法是: 初始化4*3的二维数组 a = [[0 for col in xrange(3)] for row in xrange(4)] 而不可以用: a = [[0]*3]*4 [0]*3是生 ...
- ecshop二次开发之后台秒杀
1.进入admin->includes->inc_menu.PHP中此文件为定义左侧功能模块超链接 2.添加include/inc_menu.php秒杀管理超链接找链接 $modules[ ...
- 集合-Map 接口
1. 概述 java.util.Map <K,V>接口是一个顶层接口,里面存放的数据单元是:单对元素: K 表示 描述的键 的类型,Key 的类型: V 表示 描述的值 的类型,Val ...
- Windows系统MySQL8.0的安装教程
MySQL推出的8.0版本亮点多多,尤其是两倍的提速更让我们迫不及待地安装一睹为快.然而目前我们所用的版本还在5.7之前,今天小编给家分享一下MySQL8.0的安装,尤其是多版本MySQL的共存. 方 ...