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. CentOS如何升级openssl到最新版本

    本文不再更新,可能存在内容过时的情况,实时更新请移步原文地址:CentOS如何升级openssl到最新版本: 环境信息 CentOS Linux release 7.6.1810 (Core): Op ...

  2. Highcharts 饼图数值显示在图形上

    1.引用js文件 <script type="text/javascript" src="http://cdn.hcharts.cn/jquery/jquery-1 ...

  3. TZ_15Spring-Cloud_Eureka-Ribbon-Hystix-Feign-Zuul微服务整合

    1.一个微服务框架的基本流程 2.Eureka                                   --Feign-Zuul Eureka:就是服务注册中心(可以是一个集群),对外暴露 ...

  4. linux apache vhost

    <VirtualHost *:80> DocumentRoot "/usr/www/yltgerp_old/" ServerName erp.yltg.com.cn E ...

  5. JavaScript中[]+[] 、[]+{}、{}+[]、{}+{}的结果分析

    看到这样一个问题:{} + [] 的结果是多少? 一脸懵逼.. 于是在chrome控制台运行 {} + [] 和用 console.log({} + []) 输出,发现结果不一样.. 于是,把各种可能 ...

  6. Spring的IoC容器(转)BeanFactory

    Spring的IoC容器 Spring读书笔记-----Spring的Bean之Bean的基本概念 加菲猫 Just have a little faith. Spring的IoC容器 (用户持久化类 ...

  7. 我悲惨的人生,该死的UPX壳,谁能救救我

     一个程序,被加了UPX壳... 结果加壳的人把UPX脱壳的关键参数都给删除掉了,我现在连脱壳都脱不掉... 我从网上下载了UPX最新3.91版本的壳,复制了两个UPX.exe,本来互相加壳和脱壳都没 ...

  8. react仿豆瓣

    最近公司在做一个自己内部的图片上传系统,目的是帮助设计人员方便上传图片,用的是vue技术,但是说到vue,可能要提到更早出来的react,react是facebook搞的一套语法糖,也是革命性的用组件 ...

  9. NKOJ1472 警卫安排

    P1472警卫安排   时间限制 : 10000 MS   空间限制 : 65536 KB 问题描述 一个重要的基地被分为n个连通的区域.出于某种神秘的原因,这些区域以一个区域为核心,呈一颗树形分布. ...

  10. NOIP模拟 6.30

    Problem 1 护花(flower.cpp/c/pas) [题目描述] 约翰留下他的N(N<=100000)只奶牛上山采木.他离开的时候,她们像往常一样悠闲地在草场里吃草.可是,当他回来的时 ...