Jdoj-Minimal area sub-matrix (reject binary)

Source: Internet
Author: User

This is to find the smallest area matrix in a matrix, so that its sum is greater than a given K. Because it is to find the optimal solution, it is natural to think of binary search for this value, however, on the premise that the problem does not meet the requirements of binary search, I chose binary search in a hurry, leading to errors. The reason is that when an area is an optimal solution, the Child matrix with an area greater than the optimal solution does not necessarily contain this optimal matrix, for example, a 5*5 matrix, if the optimal matrix is a 2*2 matrix, then the matrix formula with the area of 1*5 is 5 and does not contain the 2*2 matrix, So ......

The Code is as follows:

# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # define maxn 105 using namespace STD; int n, m, K, G [maxn] [maxn], s [maxn * maxn], Lim; int rec [maxn * maxn], idx, sum [maxn] [maxn]; struct node {int X, Y;} que [maxn]; int front, tail; inline void pre () {int K; Lim = N * m; memset (S, 0, sizeof (s); idx =-1; // getstatus for (INT I = 1; I <= N; ++ I) {k = min (m, lim/I); // select a relatively small value to exclude illegal conditions For (Int J = 1; j <= K; ++ J) {s [I * j] = 1 ;}} for (INT I = 1; I <= lim; ++ I) {If (s [I]) {rec [++ idx] = I ;}// getsum for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= m; ++ J) {sum [I] [J] = sum [I-1] [J] + sum [I] [J-1]-sum [I-1] [J-1] + G [I] [J] ;}}} bool judge () {int XX, YY, ZZ, lx, Ly; For (int K = 1; k <= tail; ++ K) {Lx = N-que [K]. X + 1, Ly = m-que [K]. Y + 1; for (INT I = 1; I <= Lx; ++ I) {For (Int J = 1; j <= ly; ++ J) {xx = I + que [K]. x-1, YY = J + que [K]. y-1; If (XX <= N & YY <= m) {ZZ = sum [XX] [YY]-sum [I-1] [YY]-sum [XX] [J-1] + sum [I-1] [J-1]; if (zz> = k) return true ;}}} return false;} bool AC (int x) // ensure that all x values are valid {front = tail = 0; For (INT I = 1; I <= N; ++ I) {If (X % I = 0) {++ tail; que [tail]. X = I, que [tail]. y = x/I ;}/// decomposition of the current area at the processing. If (Judge () {return True;} return false;} int bsearch (int l, int R) {int mid, ret =-1; while (L <= r) {mid = (L + r)> 1; if (AC (REC [Mid]) {ret = rec [Mid]; r = mid-1 ;} else {L = Mid + 1 ;}} return ret ;}int force () {for (INT I = 0; I <= idx; ++ I) {If (AC (REC [I]) {return rec [I] ;}} return-1 ;}int main () {While (scanf ("% d", & N, & M, & K )! = EOF) {for (INT I = 1; I <= N; ++ I) {for (Int J = 1; j <= m; ++ J) {scanf ("% d", & G [I] [J]) ;}} pre (); // printf ("% d \ n", bsearch (0, idx); printf ("% d \ n", force ();} 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.