Sgu 167. I-Country

Source: Internet
Author: User

Time Limit: 0.75 s

Space limit: 6 MB

Question:

In a grid of N * m (n, m <= 15), each grid has a value. Now K (k <= N * m) are extracted from the grid, make sure that only up to four (upper, lower, left, and right) operations can be used to remove any other lattice from the selected lattice, and maximize the value. Output the value and the selected grid coordinates.

 

 

 

Solution

There are only two operations to remove all grids from any of the selected grids:

The left graph is a set of grids that meet the conditions. The () to () of the right graph must use (left, bottom, right) three operations to REACH, which is not required.

 

Note that the grid set meeting the conditions will not be concave, because the grids in the upper and lower vertices of the concave cannot be reached only by two operations.

 

In this way, is used to record whether the left and right sides are protruding outward or contracting inward.

When one side of a row shrinks inward, it cannot be highlighted outward.

Next we will use line-based DP

F [row] [l] [r] [OPL] [OPR] [K], representing the row where the column L carries the lattice of the column R, the highlight status of the left-side grid is OPL, and the right-side is opr. Currently, K grids are selected.

 

When recording the path, it takes 15*15*2*2*225 * size (INT) * 6 space to record the previous state of all States with struct.

After a memory burst, You can launch row and K in the previous state. You only need to record (L, R, OPL, OPR), and (L, R, OPL, OPR) if the number is no more than 16, an int can be saved in hexadecimal notation.

Use record = (L <12) + (r <8) + (OPL <4) + (OPR)

When used

Previous (row, L, R, OPL, opr, K)

It can be set by the current (Row-1, (record> 12) % 16, (record> 8) % 16, (record> 4) % 16, record % 16, k-R + L-1) to obtain

There are many things to pay attention to in this question. I will not list them one by one. In general, it is a good question worth doing.

Reference code:

# Include <iostream> # include <cstring> # include <cstdio> # define pdx pr [row] [l] [r] [OPL] [OPR] [k] # define Val (R, a, B) g [r] [B]-G [r] [A-1] Using namespace STD; int f [16] [16] [16] [2] [2] [230], G [16] [16]; int PR [16] [16] [16] [2] [2] [230]; int n, m, K, ans; int A, Al, AR, AOL, AOR;/* op = 0/left burst op = 1 \ right burst * // The current status is updated downward void Update (INT row, int L, int R, int OPL, int opr, int s) {If (ROW = N) return; For (int nl = (OPL = = 0? 1: l); NL <= r; NL ++) for (INT Nr = max (L, NL); NR <= (OPR = 0? R: m); NR ++) {int T1, T2; If (NL = L) T1 = OPL; elset1 = (NL <L? 0: 1); If (Nr = r) T2 = OPR; elset2 = (NR <r? 0: 1); If (F [row + 1] [NL] [Nr] [T1] [T2] [S + (NR-nl + 1)] <F [row] [l] [r] [OPL] [OPR] [s] + val (row + 1, NL, NR )) {f [row + 1] [NL] [Nr] [T1] [T2] [S + (NR-nl + 1)] = f [row] [l] [r] [OPL] [OPR] [s] + val (row + 1, NL, NR); // record the previous state, hexadecimal state compression PR [row + 1] [NL] [Nr] [T1] [T2] [S + (NR-nl + 1)] = (L <12) + (r <8) + (OPL <4) + (OPR) ;}} void dp () {for (int row = 1; row <= N; row ++) for (int l = 1; L <= m; l ++) for (INT r = L; r <= m; r ++) for (int opl = 0; OPL <2; OPL ++) for (int opr = 0; OPR <2; OPR ++) {f [row] [l] [r] [OPL] [OPR] [R-l + 1] = G [row] [r]-G [row] [l- 1]; for (int s = r-L + 1; S <= K; s ++) if (F [row] [l] [r] [OPL] [OPR] [s]> 0) {Update (row, L, R, OPL, opr, S ); if (S = k) // Record maximum solution if (F [row] [l] [r] [OPL] [OPR] [s]> ans) {ans = f [row] [l] [r] [OPL] [OPR] [s]; A = row, Al = L, AR = r, AOL = OPL, AOR = OPR ;}}}// output void write (INT row, int L, int R, int OPL, int opr, int K) {If (ROW = 0 | K <= 0) return; For (INT I = L; I <= r; I ++) printf ("% d \ n", row, I); write (Row-1, (PDX> 12) % 16, (PDX> 8) % 16, (PDX> 4) % 16, PDX % 16, K-R + L-1);} int main () {# ifndef online_judgefreopen ("in.txt", "r ", stdin); # endif // oline_judgescanf ("% d", & N, & M, & K); For (INT I = 1; I <= N; I ++) for (Int J = 1; j <= m; j ++) {scanf ("% d", & G [I] [J]); G [I] [J] + = G [I] [J-1];} memset (F,-1, sizeof F); DP (); printf ("oil: % d \ n ", ANS); write (A, Al, AR, AOL, AOR, k); 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.