Poj2559 largest rectangle in a histogram hdu1505 City game

Source: Internet
Author: User

Poj2559

Question:

Find the largest rectangle in the Histogram


, The input data is

7 2 1 4 5 1 3 3
7 is the number, and the histogram is as follows:

Enumerate all rectangles with a width of 1 and expand them to the two sides to find the rectangle with its complete and largest area.

If the k-th rectangle with a width of 1 is specified, the rectangle with a width of 1 can be expanded to the LK rectangle on the left, to the right side, you can expand to the rectangle where the rk width is 1. You can use a monotonic queue to solve Lk And rk.

Maintain an increasing monotonous queue. The queue element is (height, location)

Initially, () is added to the queue. When () is added to the queue, () is required. For (), the maximum extension on the right is () as the boundary.

# Include <iostream> # include <algorithm> # include <cstdio> # include <deque> using namespace STD; long H [100010]; long Rh [100010]; long long LH [100010]; struct t {long height; long index;}; int main () {long n; while (scanf ("% LLD ", & N), n) {for (long I = 0; I <n; I ++) {scanf ("% LLD", & H [I]); LH [I] = 0; Rh [I] = n-1;} deque <t> q; For (long I = 0; I <n; I ++) // maintain an incremental monotonous queue and obtain the boundary for each square to expand to the right {While (! Q. empty () & Q. back (). height> H [I]) {Rh [q. back (). index] = I-1; q. pop_back ();} t tt; TT. height = H [I]; TT. index = I; q. push_back (TT);} Q. clear (); For (long I = n-1; I> = 0; I --) // maintain an incremental monotonous queue and obtain the boundary of each square extending to the left {While (! Q. empty () & Q. back (). height> H [I]) {LH [q. back (). index] = I + 1; q. pop_back ();} t tt; TT. height = H [I]; TT. index = I; q. push_back (TT);} long ans =-1; for (long I = 0; I <n; I ++) {If (Rh [I]-lH [I] + 1) * H [I]> ans) {ans = (Rh [I]-lH [I] + 1) * H [I] ;}} printf ("% LLD \ n", ANS) ;}return 0 ;}

Hdu1505

Question:

A rectangle is given. The length and width of the rectangle are smaller than 1000.

The rectangle is filled with R and F.

Divide the largest hold in this hold, and this rectangle does not contain R. the area of this rectangle × 3 is the answer.

Solution:

Example matrix:

R f

F

R f

F

F

For the first row, convert it to numbers 0, 1, 1, 1, and 1.

For the second row, convert it to numbers 0, 2, 2, 2, 2

The rule is based on the row and the maximum height to the top.

Then we need to find the largest rectangle in this one-dimensional histogram, that is, the question of poj2559.

Code:

# Include <iostream> # include <algorithm> # include <cstdio> # include <deque> using namespace STD; int A [1100] [1100]; int ant; int N; int Lp [1100]; int RP [1100]; struct t {int num; int index;}; int calc (int * P) // pair P [0], P [1], p [n-1] returns the maximum rectangle {for (INT I = 0; I <n; I ++) {Lp [I] = 0; RP [I] = n-1;} deque <t> q; // maintain an incremental queue and obtain rpfor (INT I = 0; I <n; I ++) {While (! Q. empty () & Q. back (). num> P [I]) {RP [q. back (). index] = I-1; q. pop_back ();} t tt; TT. index = I; TT. num = P [I]; q. push_back (TT);} Q. clear (); For (INT I = n-1; I> = 0; I --) {While (! Q. empty () & Q. back (). num> P [I]) {Lp [q. back (). index] = I + 1; q. pop_back ();} t tt; TT. index = I; TT. num = P [I]; q. push_back (TT);} int ans = 0; For (INT I = 0; I <n; I ++) {If (ANS <(RP [I]-Lp [I] + 1) * P [I]) {ans = (RP [I]-Lp [I] + 1) * P [I] ;}}return ans ;}int main () {int total; scanf ("% d", & total); While (total --) {ant = 0; int m; scanf ("% d", & M, & N); char ch; For (INT I = 0; I <m; I ++) {for (Int J = 0; j <n; j ++) {CIN> CH; If (CH = 'F ') {if (I = 0) {A [I] [J] = 1 ;} else {A [I] [J] = A [I-1] [J] + 1 ;}} else {A [I] [J] = 0 ;}}} for (INT I = 0; I <m; I ++) {int T = calc (A [I]); If (ANT <t) {ant = T ;}} printf ("% d \ n", Ant * 3);} 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.