CCF Test (Java)-the largest rectangle
Questions:
Question number: |
201312-3 |
Question Name: |
The largest rectangle |
Time limit: |
1.0s |
Memory Limit: |
256.0MB |
Problem Description: |
The problem description puts n adjacent rectangles on the horizontal axis, the width of each rectangle is 1, and the height of the first (1≤i≤n) rectangle is H i. These n rectangles form a histogram. For example, the height of the six rectangles in the following figure is 3, 1, 6, 5, 2, 3.
Find the largest rectangle that can be placed in a given histogram, with its edges parallel to the axis. For the example given above, the largest rectangle is the shaded part shown in the following figure, with an area of 10. Input format the first row contains an integer n, the number of rectangles (1≤n≤1000). The second row contains n integers h 1, H 2, ..., h N, and the adjacent numbers are separated by spaces. (1≤h i≤10000). H i is the height of the first rectangle. The output format outputs a row that contains an integer, which is the area of the largest rectangle within the given histogram. Sample Input 6 3 1 6 5 2 3 Example output 10 |
Answer:
import java.util.*;
public class Main {public static void main (string[] args) {Scanner input = new Scanner (system.in);
String str[] = new STRING[2];
int e = 0;
while (E<2) {str[e++] = Input.nextline ();
int n = integer.parseint (str[0]);
string[] numbers = Str[1].split ("");
int[] Nums = new Int[numbers.length];
for (int i = 0; i < nums.length i++) {Nums[i] = Integer.parseint (numbers[i));
int result = 0,temp = 0;
Boolean entry = true;
for (int i = 0; i < nums.length i++) {int l=0,r=0;
for (int p=i-1;p>=0;p--) {if (Nums[i]<nums[p]) {l++;
}else{break;
} for (int q=i+1;q<nums.length;q++) {if (Nums[i]<nums[q]) {r++;
}else{break;
} if (entry) {temp = result = nums[i]* (l+r+1);
Entry = false;
}else{temp = nums[i]* (l+r+1);
if (temp > result) {result = temp;
}} System.out.println (Result); }
}
Results (don't know why only 90 points):