LA 3029 City Game (DP||单调栈)
来源:互联网 发布:hp3055扫描仪驱动软件 编辑:程序博客网 时间:2024/06/05 19:19
City Game
http://acm.hdu.edu.cn/showproblem.php?pid=1505
Problem Description
Bob is a strategy game programming specialist. In his new city building game the gaming environment is as follows: a city is built up by areas, in which there are streets, trees,factories and buildings. There is still some space in the area that is unoccupied. The strategic task of his game is to win as much rent money from these free spaces. To win rent money you must erect buildings, that can only be rectangular, as long and wide as you can. Bob is trying to find a way to build the biggest possible building in each area. But he comes across some problems – he is not allowed to destroy already existing buildings, trees, factories and streets in the area he is building in.
Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you're building stands is 3$.
Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F.
Each area has its width and length. The area is divided into a grid of equal square units.The rent paid for each unit on which you're building stands is 3$.
Your task is to help Bob solve this problem. The whole city is divided into K areas. Each one of the areas is rectangular and has a different grid size with its own length M and width N.The existing occupied units are marked with the symbol R. The unoccupied units are marked with the symbol F.
Input
The first line of the input contains an integer K – determining the number of datasets. Next lines contain the area descriptions. One description is defined in the following way: The first line contains two integers-area length M<=1000 and width N<=1000, separated by a blank space. The next M lines contain N symbols that mark the reserved or free grid units,separated by a blank space. The symbols used are:
R – reserved unit
F – free unit
In the end of each area description there is a separating line.
R – reserved unit
F – free unit
In the end of each area description there is a separating line.
Output
For each data set in the input print on a separate line, on the standard output, the integer that represents the profit obtained by erecting the largest building in the area encoded by the data set.
Sample Input
25 6R F F F F FF F F F F FR R R F F FF F F F F FF F F F F F5 5R R R R RR R R R RR R R R RR R R R RR R R R R
Sample Output
450
解:从上到下遍历,维护每一个点能延伸的左右和上边界
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<cmath>#include<string>#include <bits/stdc++.h>using namespace std;const int N = 1e4+10;char str[1100][1100];int s[1100][1100], up[1100][1100],left1[1100][1100],right1[1100][1100];int main(){ int t; scanf("%d", &t); while(t--) { int n, m; scanf("%d %d", &n, &m); for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { char ch[3]; scanf("%s",ch); if(ch[0]=='F') s[i][j]=0; else s[i][j]=1; } } memset(left1,0,sizeof(left1)); memset(right1,0,sizeof(right1)); memset(up,0,sizeof(up)); int ans=0; for(int i=1;i<=n;i++) { int l=0,r=m+1; for(int j=1;j<=m;j++) { if(s[i][j]==1) up[i][j]=0,left1[i][j]=0,l=j; else { up[i][j]=up[i-1][j]+1,left1[i][j]=i==1?l+1:max(left1[i-1][j],l+1); } } for(int j=m;j>=1;j--) { if(s[i][j]==1) right1[i][j]=m,r=j; else { right1[i][j]=i==1?r-1: min(right1[i-1][j],r-1); ans=max(ans,up[i][j]*(right1[i][j]-left1[i][j]+1)); } } } cout<<ans*3<<endl; } return 0;}
栈是后进先出
#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<cmath>#include<string>#include <bits/stdc++.h>using namespace std;const int N = 1e4+10;char str[1100][1100];int s[1100][1100], up[1100][1100],left1[1100][1100],right1[1100][1100];struct node{ int id, x;};int d[1010][1010];int main(){ int t; scanf("%d", &t); while(t--) { int n, m; scanf("%d %d", &n, &m); for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { char ch[3]; scanf("%s",ch); if(ch[0]=='F') s[i][j]=0; else s[i][j]=1; } } stack<node>st; memset(d,0,sizeof(d)); int ans=0; for(int i=1; i<=n; i++) { while(!st.empty()) st.pop(); for(int j=1; j<=m; j++) { if(s[i][j]==0) d[i][j]=d[i-1][j]+1; else d[i][j]=0; while(!st.empty()&&st.top().x>=d[i][j]) st.pop(); if(st.empty()) ans=max(ans,d[i][j]*j); else ans=max(ans,d[i][j]*(j-st.top().id)); st.push((node) { j,d[i][j] }); } while(!st.empty()) st.pop(); for(int j=m; j>=1; j--) { while(!st.empty()&&st.top().x>=d[i][j]) st.pop(); if(st.empty()) ans=max(ans,d[i][j]*(m-j+1)); else ans=max(ans,d[i][j]*(st.top().id-j)); st.push((node) { j,d[i][j] }); } } cout<<ans*3<<endl; } return 0;}
阅读全文
0 0
- LA 3029 City Game (DP||单调栈)
- LA 3029 City Game [扫描线][DP]
- LA -- 3029 City Game 【思维 + dp】
- hdu - 1505 - City Game(dp / 单调栈)
- LA 3029 - City Game
- LA 3029 City Game
- LA 3029 City Game .
- LA 3029(p50)----City Game
- hdu1505---City Game(单调栈)
- HDU1505 City Game dp+若干次单调栈
- hdoj 1505 City Game 【dp or 单调栈】
- hdu1505 City Game (单调栈)
- LA 3029 - City Game (模拟&技巧)好题
- UVA 1330 - City Game(扫描方法 | 单调栈维护)
- HDOJ 1505 City Game (单调栈)
- hdu1505 City Game--DP/栈
- hdu1505 City Game(DP)
- UVA-1330 LA 3029 City Game(最大子矩阵)
- 【设计模式】——责任链模式
- Spark学习札记
- d3 绘制中国地图
- HDU
- 对面向对象的一些思考
- LA 3029 City Game (DP||单调栈)
- 美素数 HDU
- 内存可见性和原子性:Synchronized和Volatile的比较
- 中位数的和_KEY
- spring的基本使用
- MySQL--事务
- 子命令窗口的运行情况
- android系统学习笔记——专业驱动程序
- HDU 5412 CRB and Queries 树套树 BIT+PBDS