UVa 10074

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

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int MAXN = ;
  7. int Map[MAXN][MAXN], width[MAXN][MAXN];
  8. int row, col;
  9.  
  10. int main()
  11. {
  12. while (cin>>row>>col&&!(row==&&col==))
  13. {
  14. int ans = ;
  15. for(int i=;i<=row;i++)
  16. for (int j = ; j <= col; j++) {
  17. cin >> Map[i][j];
  18. if (Map[i][j]) width[i][j] = ;
  19. else width[i][j] = width[i][j - ] + ;
  20. }
  21. for (int i = ; i <= row; i++)
  22. {
  23. for (int j = ; j <= col; j++) {
  24. int w = 1e9;
  25. for (int h = ; i - h + > ; h++) {
  26. if (Map[i][j]) break;
  27. w = min(w, width[i - h + ][j]);
  28. ans = max(ans, w*h);
  29. }
  30. }
  31. }
  32. cout << ans << endl;
  33. }
  34. return ;
  35. }

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int MAXN = ;
  7. int Map[MAXN][MAXN];
  8. int wl[MAXN], wr[MAXN];
  9. int h[MAXN], l[MAXN], r[MAXN];
  10. int row, col;
  11.  
  12. int main()
  13. {
  14. while (scanf("%d%d", &row, &col) == , !(row == && col == ))
  15. {
  16. int ans = ;
  17. for (int i = ; i <= row; i++)
  18. for (int j = ; j <= col; j++)
  19. scanf("%d", &Map[i][j]);
  20. for (int i = ; i <= row; i++)
  21. {
  22. for (int j = ; j <= col; j++)
  23. if (Map[i][j]) wl[j] = ;
  24. else wl[j] = wl[j - ] + ;
  25.  
  26. for (int j = col; j >= ; j--)
  27. if (Map[i][j]) wr[j] = ;
  28. else wr[j] = wr[j + ] + ;
  29.  
  30. for (int j = ; j <= row; j++)
  31. if (Map[i][j]) h[j] = ;
  32. else h[j] = h[j] + ;
  33.  
  34. for (int j = ; j <= col; j++)
  35. if (r[j] == ) r[j] = wr[j];
  36. else r[j] = min(wr[j], r[j]);
  37.  
  38. for (int j = ; j <= col; j++)
  39. if (l[j] == ) l[j] = wl[j];
  40. else l[j] = min(wl[j], l[j]);
  41.  
  42. for (int j = ; j <= col; j++)
  43. ans = max(ans, (l[j] + r[j] - )*h[j]);
  44. }
  45. printf("%d\n", ans);
  46. }
  47. return ;
  48. }

WA1

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<stack>
  6. using namespace std;
  7. const int MAXN = ;
  8. int a[MAXN][MAXN], h[MAXN][MAXN];
  9. int col, row;
  10.  
  11. int main()
  12. {
  13. while (cin >> row >> col)
  14. {
  15. if (!col && !row) break;
  16. for (int i = ; i <= row; i++)
  17. for (int j = ; j <= col; j++)
  18. cin >> a[i][j];
  19. memset(h, , sizeof(h));
  20. for (int i = ; i <= row; i++)
  21. for (int j = ; j <= col; j++)
  22. if (a[i][j]) h[i][j] = ;
  23. else h[i][j] = h[i - ][j] + ;
  24.  
  25. stack<int> st;
  26. int area;
  27. int mx = ;
  28. for (int i = ; i <= row; i++)
  29. {
  30. int j;
  31. for (j = ; j <= col;) {
  32. if (st.empty() || h[i][st.top()] <= h[i][j])
  33. st.push(j++);
  34. else {
  35. int top = st.top();
  36. st.pop();
  37. if (st.empty())
  38. area = h[i][top] * j;
  39. else
  40. area = h[i][top] * (j - st.top() - );
  41. mx = max(mx, area);
  42. }
  43. }
  44. while (!st.empty())
  45. {
  46. int top = st.top();
  47. st.pop();
  48. if (st.empty()) area = h[i][top] * j;
  49. else
  50. area = h[i][top] * (j - st.top() - );
  51. mx = max(mx, area);
  52. }
  53. }
  54. cout << mx << endl;
  55. }
  56. return ;
  57. }

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. bzoj 1179 [Apio2009]Atm——SCC缩点+spfa

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1179 显然SCC缩点. 然后准备倒着拓扑序推到st,结果WA. 听TJ说dj求最长路会发生不 ...

  2. android的AIDL

    一.AIDL的意义:           AIDL全称是Android Interface Definition Language,是android接口定义语言.AIDL就是为了避免我们一遍遍的写一些 ...

  3. JS简单实现:根据奖品权重计算中奖概率实现抽奖的方法

    本文主要介绍:使用 JS 根据奖品权重计算中奖概率实现抽奖的方法. 一.示例场景 1.1.设置抽奖活动的奖项名称 奖项名称:["一等奖", "二等奖", &qu ...

  4. WPF内嵌CEF控件,与JS交互

    1)安装cefsharp.winform包 打开VS2017,打开nuget,找到cefsharp.winform,安装 问:为什么wpf程序不使用cefsharp.wpf? 答:因为cefwpf 4 ...

  5. message d:\WEB_APP_QuChongFu\file\五月.xlsx (文件名、目录名或卷标语法不正确。)

    原因是 文件名或文件夹名中不能出现以下字符:\   /   :   *   ?  "  <  >   | 但是后台读取到的附件的文件路径就是这样的 网上大佬说了,这样处理 rep ...

  6. Java 类与类之间的调用

    方法1. 新建一个类. 然后在调用类中先进行被调用类实例化,然后通过实例化的对象访问. 例如: //先定义一个类 import static java.lang.System.out; public ...

  7. 学习Boost/Asio

    编译boost 在Mac下使用brew编译boost,为了使用C++11,需要加入参数–c++11 $ brew install boost --c++11 在我的Mac虚拟机里面用了20分钟左右编译 ...

  8. vue 项目重定向时需要传参数

    1.在项目首页路由因需要进行传参数,例如需要重定向到:path: "/index?from=0" 2.重定向时写法如下: redirect: {path: '/index',que ...

  9. Java IO:如何得到Jar包中内嵌Jar包的时间戳

    ClassLoader bladeClassLoader = BladeCLI.class.getClassLoader(); URL url = bladeClassLoader.getResour ...

  10. LintCode刷题笔记-- CoinsInLine

    标签: 动态规划 问题描述: There are n coins with different value in a line. Two players take turns to take one ...