博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
uva 1330 - City Game
阅读量:4073 次
发布时间:2019-05-25

本文共 2509 字,大约阅读时间需要 8 分钟。

题目链接:

以前做过一道一维的,这题只是变成了二维的,其他方法都一样。   

代码1:

#include
#include
#include
using std::stack;const int MAXN = 1010;char str[MAXN*3];int sum[MAXN][MAXN];int left[MAXN], right[MAXN];int n, m;inline void input(){ scanf("%d%d%*c", &n, &m); for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ char ch = getchar(); while(ch!='F' && ch!='R') ch=getchar(); sum[i][j] = ch=='R'?0:1; if(sum[i][j]) sum[i][j] += sum[i-1][j]; } } }inline void solve(){ input(); int ans = 0; for(int i=1; i<=n; ++i){ sum[i][0] = sum[i][m+1] = -1; for(int j=1; j<=m; ++j) left[j]=right[j]=j; for(int j=1; j<=m; ++j){ while(sum[i][j] <= sum[i][left[j]-1]) left[j] = left[left[j]-1]; } for(int j=m; j>=1; --j){ while(sum[i][j] <= sum[i][right[j]+1]) right[j] = right[right[j]+1]; int tmp = (right[j]-left[j]+1)*sum[i][j]; if(tmp > ans) ans=tmp; } } printf("%d\n", ans*3);}int main(){ int nCase; scanf("%d", &nCase); while(nCase--){ solve(); } return 0;}

代码2(栈扫描):

#include
#include
#include
using std::stack;const int MAXN = 1010;char str[MAXN*3];int sum[MAXN][MAXN];int n, m;inline void input(){ scanf("%d%d%*c", &n, &m); memset(sum, 0, sizeof(sum)); for(int i=1; i<=n; ++i){ for(int j=1; j<=m; ++j){ char ch = getchar(); while(ch!='F' && ch!='R') ch=getchar(); sum[i][j] = ch=='R'?0:1; if(sum[i][j]) sum[i][j] += sum[i-1][j]; } } }inline void solve(){ input(); int ans = 0; for(int i=1; i<=n; ++i){ sum[i][0] = sum[i][m+1] = -1; stack
q; for(int j=1; j<=m+1; ++j){ if(q.empty() || sum[i][j]>sum[i][q.top()]){ q.push(j); } else if(sum[i][j] < sum[i][q.top()]){ int pos; while(!q.empty() && sum[i][q.top()]>sum[i][j]){ int tmp = (j-q.top())*sum[i][q.top()]; if(tmp > ans) ans = tmp; pos = q.top(); q.pop(); } q.push(pos); sum[i][pos] = sum[i][j]; } } } printf("%d\n", ans*3);}int main(){ int nCase; scanf("%d", &nCase); while(nCase--){ solve(); } return 0;}

转载地址:http://vszni.baihongyu.com/

你可能感兴趣的文章
nodejs内存控制
查看>>
nodejs Stream使用中的陷阱
查看>>
MongoDB 数据文件备份与恢复
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>
memcached工作原理与优化建议
查看>>
Redis与Memcached的区别
查看>>
程序员最核心的竞争力是什么?
查看>>
linux CPU个数查看
查看>>
分布式应用开发相关的面试题收集
查看>>
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
分布式存储系统设计(1)—— 系统架构
查看>>
MySQL数据库的高可用方案总结
查看>>
常用排序算法总结(一) 比较算法总结
查看>>
SSH原理与运用
查看>>