• HDU 1506 & 1505


    Largest Rectangle in a Histogram

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 17635    Accepted Submission(s): 5261

     
    Problem Description
    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles: 
     
    Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.

    Input

    The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, ..., hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. A zero follows the input for the last test case.

    Output

    For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.

    Sample Input

    7 2 1 4 5 1 3 3
    4 1000 1000 1000 1000
    0

    Sample Output

    8
    4000
    对于a[i],我们需要记录的是他之前和之后最大的连续的值比a[i]大的长度,如果每次都一个个去比对,数据大小是100000,时间复杂度是O(n^2),超时那是必然的。但是其实对于a[i]来说,如果去求后面连续的值,完全没必要一个个去比对,直接看a[i-1]的值就行了。
    
    比如说2、3、4、5这个序列,如果我们要看3往后能延伸多长,并不需要去逐个和4和5比较,在计算4的时候,我们已经计算过5是比4大的,因为3比4小,4所能往后延伸的长度,3也一定能达到(4能延伸的长度内的数据都大于等于4,当然也都比3大),我们可以直接比较在4达到的最终长度的末端之后的值。
    
    这道题计算的时候进制转换也需要特别注意,如果temp没有强制转换成__int64位,提交会wa。
    
    这道题优化的思路非常巧妙,很值得学习。

    参照这个思路,用dp求h[i]的左右边界,

    先初始化h[i]的left[i]和right[i]都是i

    对于第i个数h[i],假设它右边的所有数字的right[]都已经得到,那么比较h[i]和h[ right[i]+1 ]的大小,如果h[i] <= h[ right[i]+1 ],那么把right[i]变成right[i]+1的高度,再循环前面的步骤即可,直到right[i]+1到达整个h数组的边界即停止。

    类似的可得到left[i]。

     1 #include<cstdio>
     2 int main()
     3 {  
     4     int n,h[100003],left[100003],right[100003];  
     5     while(1){  
     6         scanf("%d",&n);  
     7         if(n<=0) break;  
     8         for(int i=1;i<=n;i++){  
     9             scanf("%d",&h[i]);  
    10             right[i]=left[i]=i;  
    11         }  
    12         for(int i=n-1;i>=1;i--){  
    13             while(right[i]+1<=n && h[i]<=h[right[i]+1]) right[i]=right[right[i]+1];  
    14         }  
    15         for(int i=2;i<=n;i++){  
    16             while(left[i]-1>=1 && h[i]<=h[left[i]-1]) left[i]=left[left[i]-1];  
    17         }  
    18         unsigned long long ans=0;  
    19         for(int i=1;i<=n;i++)  
    20         {   
    21             unsigned long long temp=(unsigned long long)(right[i]-left[i]+1) * h[i];   
    22             if(temp>ans) ans=temp;   
    23         }   
    24         printf("%llu
    ",ans);  
    25     }  
    26     return 0;  
    27 } 

    City Game

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6835    Accepted Submission(s): 2956

    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.
     
    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.
      
    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
    2
    5 6
    R F F F F F
    F F F F F F
    R R R F F F
    F F F F F F
    F F F F F F
    5 5
    R R R R R
    R R R R R
    R R R R R
    R R R R R
    R R R R R
     
    Sample Output
    45 0

    上面一题的进阶版,懒了……不想写思路了……

     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 int deep[1003][1003];
     5 int l[1003][1003];
     6 int r[1003][1003];
     7 int main()
     8 {
     9     int t;
    10     scanf("%d",&t);
    11     while(t--){
    12         int m,n;char temp;
    13         scanf("%d%d",&m,&n);
    14         for(int j=1;j<=n;j++) deep[0][j]=0;
    15         for(int i=1;i<=m;i++){
    16             for(int j=1;j<=n;j++){
    17                 
    18                 cin>>temp;
    19                 if(temp=='F')
    20                 {
    21                     deep[i][j]=deep[i-1][j]+1;
    22                 } 
    23                 else if(temp=='R')
    24                 {
    25                     deep[i][j]=0;
    26                 }
    27                 
    28                 l[i][j]=j;r[i][j]=j;
    29             }
    30         }
    31         
    32         for(int i=1;i<=m;i++){
    33             for(int j=n-1;j>=1;j--){
    34                 while( r[i][j]+1<=n && deep[i][j]<=deep[i][ (r[i][j]+1) ] ) r[i][j]=r[i][ (r[i][j]+1) ];
    35             }
    36             for(int j=2;j<=n;j++){
    37                 while( l[i][j]-1>=1 && deep[i][j]<=deep[i][ (l[i][j]-1) ] ) l[i][j]=l[i][ (l[i][j]-1) ];
    38             }
    39         }
    40         
    41         int ans=0;
    42         for(int i=1;i<=m;i++){
    43             for(int j=1;j<=n;j++){
    44                 int temp=deep[i][j]*(r[i][j]-l[i][j]+1);
    45                 if(ans<temp) ans=temp;
    46             }
    47         }
    48         printf("%d
    ",ans*3);
    49     }
    50 }
  • 相关阅读:
    React 进修之路(1)
    requireJS的配置心得
    DOM的内部插入和外部插入
    h5移动端设备像素比dpr介绍
    原生js--事件类型
    React 进修之路(3)
    javaScript
    html&css
    MyBatis
    Maven基础
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804183.html
Copyright © 2020-2023  润新知