Ultraviolet A 1330-City Game (scan method | monotonous stack maintenance)

Source: Internet
Author: User

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 on Ly 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 has SS 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.

Input The first line of the input file 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.

Output For each data set in the input file 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

Question: R is the occupied position, F is the vacant space, and the maximum size of the sub-null matrix is obtained * 3.

Train of Thought: 1. The suspension method records the farthest position on the left and right of the Suspension Line at each position. Then, the maximum maintenance area is maintained.

2. monotonous stack maintenance. During enumeration at each position, the stack is added from left to right, increasing the number of maintenance stacks. If there is a conflict, the previous non-incremental calculation is finished. Note that after all the elements are added to the stack, the elements in the stack should be calculated.

Code:

Suspension method:

#include 
 
  #include 
  
   const int N = 1005;int max(int a, int b) {return a>b?a:b;}int min(int a, int b) {return a
   
    = 1; j--) {while (up[i][j] <= up[i][r[j] + 1])r[j] = r[j] + 1;ans = max(ans, up[i][j] * (r[j] - l[j] + 1));}}return ans * 3;}int main() {scanf("%d", &t);while (t--) {init();printf("%d\n", solve());}return 0;}
   
  
 

Monotonous stack maintenance:

#include 
 
  #include 
  
   #include 
   
    using namespace std;#define INF 0x3f3f3f3fconst int N = 1005;int max(int a, int b) {return a>b?a:b;}int min(int a, int b) {return a
    
      st;for (int j = 1; j <= m + 1; j++) {if (st.empty() || sum[i][j] > sum[i][st.top()])st.push(j);else if (sum[i][j] < sum[i][st.top()]) {int pos;while (!st.empty() && sum[i][j] < sum[i][st.top()]) {ans = max(ans, (j - st.top()) * sum[i][st.top()]);pos = st.top();st.pop();}st.push(pos);sum[i][pos] = sum[i][j];}}}return ans * 3;}int main() {scanf("%d%", &t);while (t--) {init();printf("%d\n", solve());}return 0;}
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.