https://ac.nowcoder.com/acm/contest/882/H

正确的办法:dp1[i][j]表示以i,j为底的矩形的高。得到dp1之后,dp2[i][j]表示以dp1[i][j]悬线向左能移动的极限(用单调栈)。

维护最后答案的时候单调栈是>=的,这样同高的就不会重复计算。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; #define ERR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {cerr << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << "=" << a << ", ";
err(++it, args...);
} #define ERR1(arg,n) { cerr<<""<<#arg<<"=\n "; for(int i=1;i<=n;i++) cerr<<arg[i]<<" "; cerr<<"\n"; }
#define ERR2(arg,n,m) { cerr<<""<<#arg<<"=\n"; for(int i=1;i<=n;i++) { cerr<<" "; for(int j=1;j<=m;j++)cerr<<arg[i][j]<<" "; cerr<<"\n"; } } #define REP(i, a, b) for(int i = a; i <= b; ++i) int n, m;
char g[1005][1005];
int dp1[1005][1005], dp2[1005][1005];
int stk1[1005], top;
int max1, max2; void update(int val) {
if(val > max2)
max2 = val;
if(max2 > max1) {
int t = max2;
max2 = max1;
max1 = t;
}
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
//freopen("Yinku.out", "w", stdout);
//freopen("Yinku.err", "w", stderr);
#endif // Yinku
scanf("%d%d", &n, &m);
REP(i, 1, n) scanf("%s", g[i] + 1);
REP(j, 1, m) dp1[1][j] = (g[1][j] - '0');
REP(i, 2, n) REP(j, 1, m) dp1[i][j] = (g[i][j] - '0') ? (dp1[i - 1][j] + 1) : 0; REP(i, 1, n) {
dp2[i][1] = 1;
top=0;
stk1[++top]=1;
REP(j, 2, m) {
int tmp=j;
while(top&&dp1[i][stk1[top]]>=dp1[i][j]){
tmp=stk1[top];
--top;
}
dp2[i][j]=tmp<j?dp2[i][tmp]:j;
stk1[++top]=j;
}
}
//ERR2(dp1, n, m);
//ERR2(dp2, n, m);
max1 = 0, max2 = 0;
REP(i, 1, n) {
top = 0;
REP(j, 1, m) {
while(top && dp1[i][stk1[top]] >= dp1[i][j]) {
int h = dp1[i][stk1[top]];
int w = j-1-dp2[i][stk1[top]]+1;
//ERR(stk1[top],w*h);
if(w && h) {
update(w * h);
update((w - 1) *h);
update(w * (h - 1));
}
--top;
}
stk1[++top] = j;
}
while(top) {
int h = dp1[i][stk1[top]];
int w = m-dp2[i][stk1[top]]+1;
//ERR(stk1[top],w*h);
if(w && h) {
update(w * h);
update((w - 1) *h);
update(w * (h - 1));
}
--top;
}
//cerr<<endl;
}
printf("%d\n", max2);
}

小心这个样例

5 6
110000
011000
101100
110100
111010
5 6
110010
011111
101110
110111
111010

错误代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; #define ERR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {cerr << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << "=" << a << ", ";
err(++it, args...);
} #define ERR1(arg,n) { cerr<<""<<#arg<<"=\n "; for(int i=1;i<=n;i++) cerr<<arg[i]<<" "; cerr<<"\n"; }
#define ERR2(arg,n,m) { cerr<<""<<#arg<<"=\n"; for(int i=1;i<=n;i++) { cerr<<" "; for(int j=1;j<=m;j++)cerr<<arg[i][j]<<" "; cerr<<"\n"; } } #define REP(i, a, b) for(int i = a; i <= b; ++i) int n, m;
char g[1005][1005];
int dp[1005][1005];
int stk[1005], top;
int max1, max2; void update(int val) {
if(val > max2)
max2 = val;
if(max2 > max1) {
int t = max2;
max2 = max1;
max1 = t;
}
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
scanf("%d%d",&n,&m);
REP(i, 1, n) scanf("%s", g[i] + 1);
REP(j, 1, m) dp[1][j] = (g[1][j] - '0');
REP(i, 2, n) REP(j, 1, m) dp[i][j] = (g[i][j] - '0') ? (dp[i - 1][j] + 1) : 0;
ERR2(dp, n, m);
max1 = 0, max2 = 0;
REP(i, 1, n) {
top = 0;
REP(j, 1, m) {
while(top && dp[i][stk[top]] > dp[i][j]) {
int h = dp[i][stk[top]];
int w = (j - 1) - stk[top]+1;
if(w && h) {
update(w * h);
update((w - 1) *h);
update(w * (h - 1));
}
--top;
}
stk[++top] = j;
}
while(top) {
int h = dp[i][stk[top]];
int w = m - stk[top];
if(w && h) {
update(w * h);
update((w - 1) *h);
update(w * (h - 1));
}
--top;
}
}
printf("%d\n", max2);
}

错误在于虽然把3弹出来了但其实2是可以延伸过去的。也就是单调栈一直弹出东西的时候都是可以延伸过去的,那干脆演一波算了。

虽然通过了错误代码1,但是没有计算悬线向左最远距离的方法是错的。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; #define ERR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {cerr << "\n";}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << "=" << a << ", ";
err(++it, args...);
} #define ERR1(arg,n) { cerr<<""<<#arg<<"=\n "; for(int i=1;i<=n;i++) cerr<<arg[i]<<" "; cerr<<"\n"; }
#define ERR2(arg,n,m) { cerr<<""<<#arg<<"=\n"; for(int i=1;i<=n;i++) { cerr<<" "; for(int j=1;j<=m;j++)cerr<<arg[i][j]<<" "; cerr<<"\n"; } } #define REP(i, a, b) for(int i = a; i <= b; ++i) int n, m;
char g[1005][1005];
int dp1[1005][1005], dp2[1005][1005];
int stk1[1005], top;
int max1, max2; void update(int val) {
if(val > max2)
max2 = val;
if(max2 > max1) {
int t = max2;
max2 = max1;
max1 = t;
}
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
scanf("%d%d", &n, &m);
REP(i, 1, n) scanf("%s", g[i] + 1);
REP(j, 1, m) dp1[1][j] = (g[1][j] - '0');
REP(i, 2, n) REP(j, 1, m) dp1[i][j] = (g[i][j] - '0') ? (dp1[i - 1][j] + 1) : 0;
REP(i, 1, n) dp2[i][1] = 1;
REP(i, 1, n) REP(j, 2, m) dp2[i][j] = (dp1[i][j] <= dp1[i][j - 1]) ? (dp2[i][j - 1] + 1) : 1;
ERR2(dp1, n, m);
ERR2(dp2, n, m);
max1 = 0, max2 = 0;
REP(i, 1, n) {
top = 0;
REP(j, 1, m) {
while(top && dp1[i][stk1[top]] > dp1[i][j]) {
int h = dp1[i][stk1[top]];
int w = dp2[i][stk1[top]];
if(w && h) {
update(w * h);
update((w - 1) *h);
update(w * (h - 1));
}
--top;
}
stk1[++top] = j;
}
while(top) {
int h = dp1[i][stk1[top]];
int w = dp2[i][stk1[top]];
if(w && h) {
update(w * h);
update((w - 1) *h);
update(w * (h - 1));
}
--top;
}
}
printf("%d\n", max2);
}

2019牛客暑期多校训练营(第二场) - H - Second Large Rectangle - dp的更多相关文章

  1. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  2. 2020牛客暑期多校训练营 第二场 K Keyboard Free 积分 期望 数学

    LINK:Keyboard Free 我要是会正经的做法 就有鬼了. 我的数学水平没那么高. 三个同心圆 三个动点 求围成三角形面积的期望. 不会告辞. 其实可以\(n^2\)枚举角度然后算出面积 近 ...

  3. 2020牛客暑期多校训练营 第二场 J Just Shuffle 置换 群论

    LINK:Just Shuffle 比较怂群论 因为没怎么学过 置换也是刚理解. 这道题是 已知一个置换\(A\)求一个置换P 两个置换的关键为\(P^k=A\) 且k是一个大质数. 做法是李指导教我 ...

  4. 2020牛客暑期多校训练营 第二场 I Interval 最大流 最小割 平面图对偶图转最短路

    LINK:Interval 赛时连题目都没看. 观察n的范围不大不小 而且建图明显 考虑跑最大流最小割. 图有点稠密dinic不太行. 一个常见的trick就是对偶图转最短路. 建图有点复杂 不过建完 ...

  5. 2020牛客暑期多校训练营 第二场 C Cover the Tree 构造 贪心

    LINK:Cover the Tree 最受挫的是这道题,以为很简单 当时什么都想不清楚. 先胡了一个树的直径乱搞的贪心 一直过不去.后来意识到这类似于最经典长链剖分优化贪心的做法 然后那个是求最大值 ...

  6. 2020牛客暑期多校训练营 第二场 B Boundary 计算几何 圆 已知三点求圆心

    LINK:Boundary 计算几何确实是弱项 因为好多东西都不太会求 没有到很精通的地步. 做法很多,先说官方题解 其实就是枚举一个点 P 然后可以发现 再枚举一个点 然后再判断有多少个点在圆上显然 ...

  7. 2020牛客暑期多校训练营 第二场 A All with Pairs 字符串hash KMP

    LINK:All with Pairs 那天下午打这个东西的时候状态极差 推这个东西都推了1个多小时 (比赛是中午考试的我很困 没睡觉直接开肝果然不爽 一开始看错匹配的位置了 以为是\(1-l\)和\ ...

  8. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  9. [题解] 2019牛客暑期多校第三场H题 Magic Line

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:二维平面上有n个不同的点,构造一条直线把平面分成两个点数相同的部分. 题解:对这n个点以x为第一关键 ...

  10. 2020牛客暑假多校训练营 第二场 H Happy Triangle set 线段树 分类讨论

    LINK:Happy Triangle 这道题很容易. 容易想到 a+b<x a<x<b x<a<b 其中等于的情况在第一个和第三个之中判一下即可. 前面两个容易想到se ...

随机推荐

  1. GitHub 搭建博客,出现 hexo g -d 报错

    想搭建一个个人博客,但是在将博客推送到Github上的时候在git bash 下运行hexo g -d命令出现错误: 错误如下:  fatal: HttpRequestException encoun ...

  2. 如何启用Nginx的status功能,查看服务器状态信息?

    如何查看服务器状态信息? 我们可以通过安装Nginx的功能模块,并修改Nginx的主配置文件来实现. 1.编译安装时使用--with-http_stub_status_module开启状态页面模块 [ ...

  3. Nginx优化总结

    目录 Nginx性能优化概述 一. 压力测试工具实战 二.了解影响性能指标 三.系统性能优化 四.静态资源优化 Nginx性能优化概述 基于Nginx性能优化,那么在性能优化这一章,我们将分为如下几个 ...

  4. gevent简介

    gevent是基于协程的Python网络库. 协程存在的意义:对于多线程应用,CPU通过切片的方式来切换线程间的执行,线程切换时需要耗时(保存状态,下次继续).协程,则只使用一个线程,在一个线程中规定 ...

  5. Oracle 数字转为字符串 to_char()

    格式:TO_CHAR(number,'format_model') 9 -->Represents a number 0 --> Forces a zero to be displayed ...

  6. Vue 组件间的传值(通讯)

    组件之间的通讯分为三种 父给子传 子给父传 兄弟组件之间的通讯 1 父组件给子组件传值 子组件嵌套在父组件内部,父组件给子组件传递一个标识,在子组件内部用props接收,子组件在模板里可以通过{{}} ...

  7. Thread类与Runnable接口

    Runnable 先看看源码中对Runnable的声明 @FunctionalInterface public interface Runnable { /** * When an object im ...

  8. 029:url标签使用详解

    url标签使用详解: 在模版中,我们经常要写一些 url ,比如某个 a 标签中需要定义 href 属性.当然如果通过硬编码的方式直接将这个 url 写死在里面也是可以的.但是这样对于以后项目维护可能 ...

  9. linux运维、架构之路-Nginx提高

    一.虚拟主机搭建 1.基于域名的虚拟主机 [root@web01 html]# cat nginx.conf worker_processes ; events { worker_connection ...

  10. nodejs廖雪峰大神教程

    https://www.liaoxuefeng.com/wiki/1022910821149312/1023025235359040