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 ...
随机推荐
- bzoj 1179 [Apio2009]Atm——SCC缩点+spfa
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...
- android的AIDL
一.AIDL的意义: AIDL全称是Android Interface Definition Language,是android接口定义语言.AIDL就是为了避免我们一遍遍的写一些 ...
- JS简单实现:根据奖品权重计算中奖概率实现抽奖的方法
本文主要介绍:使用 JS 根据奖品权重计算中奖概率实现抽奖的方法. 一.示例场景 1.1.设置抽奖活动的奖项名称 奖项名称:["一等奖", "二等奖", &qu ...
- WPF内嵌CEF控件,与JS交互
1)安装cefsharp.winform包 打开VS2017,打开nuget,找到cefsharp.winform,安装 问:为什么wpf程序不使用cefsharp.wpf? 答:因为cefwpf 4 ...
- message d:\WEB_APP_QuChongFu\file\五月.xlsx (文件名、目录名或卷标语法不正确。)
原因是 文件名或文件夹名中不能出现以下字符:\ / : * ? " < > | 但是后台读取到的附件的文件路径就是这样的 网上大佬说了,这样处理 rep ...
- Java 类与类之间的调用
方法1. 新建一个类. 然后在调用类中先进行被调用类实例化,然后通过实例化的对象访问. 例如: //先定义一个类 import static java.lang.System.out; public ...
- 学习Boost/Asio
编译boost 在Mac下使用brew编译boost,为了使用C++11,需要加入参数–c++11 $ brew install boost --c++11 在我的Mac虚拟机里面用了20分钟左右编译 ...
- vue 项目重定向时需要传参数
1.在项目首页路由因需要进行传参数,例如需要重定向到:path: "/index?from=0" 2.重定向时写法如下: redirect: {path: '/index',que ...
- Java IO:如何得到Jar包中内嵌Jar包的时间戳
ClassLoader bladeClassLoader = BladeCLI.class.getClassLoader(); URL url = bladeClassLoader.getResour ...
- LintCode刷题笔记-- CoinsInLine
标签: 动态规划 问题描述: There are n coins with different value in a line. Two players take turns to take one ...