[Dynamic statement] maximum privilege rectangle

Source: Internet
Author: User
The topic description is given a positive integer n (n <= 100), and then input an N * n matrix. Calculate the maximum weighted rectangle in the matrix, that is, each element of the matrix has a weight value, which is defined in the Integer Set. Find a rectangle from it. The rectangle size is unlimited, which is the sum of all elements in it. Each element of the matrix belongs to [-127,127]. For example, 0-2-7. 0 is in the lower left corner: 9 29 2-6 2-4 1-4 1-4 1-1 8-1 8 0-2 and 15 input format First line: N, next is the matrix of N rows and n columns. Sum of the largest rectangle (Child matrix) in the output format. Sample input 4 0-2-7 0 9 2-6 2-4 1-4 1-1 8 0-2 sample output 15

The largest child rectangle is similar to eating watermelon.

Accode:

# Include <cstdio> # include <cstring> # include <cstdlib> # include <bitset> Using STD: Max; const char fi [] = "rqnoj106.in "; const char fo [] = "rqnoj106.out"; const int maxn = 110; const int max = 0x3fffff00; const int min =-Max; int sum [maxn] [maxn]; int W [maxn] [maxn]; int N, ans; void init_file () {freopen (FI, "r", stdin); freopen (FO, "W ", stdout);} void readdata () {scanf ("% d", & N); For (INT I = 1; I <n + 1; ++ I) for (Int J = 1; j <n + 1; ++ J) scanf ("% d", & W [I] [J]);} void work () {for (INT I = 1; I <n + 1; ++ I) for (Int J = 1; j <n + 1; ++ J) sum [I] [J] = sum [I-1] [J] + W [I] [J]; ans = 0; For (INT I1 = 0; i1 <n; ++ I1) for (INT I2 = I1 + 1; I2 <n + 1; ++ I2) {int max = sum [I2] [1]-sum [I1] [1]; int TMP = max; For (Int J = 2; j <n + 1; ++ J) {TMP = max (0, TMP); TMP + = sum [I2] [J]-sum [I1] [J]; max = max (max, TMP);} ans = max (max, ANS);} printf ("% d", ANS);} int main () {init_file (); readdata (); work (); exit (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.