Sub-land issues (optimization of traversal problems)

Source: Internet
Author: User
Tags bool
problem
Title Description
Cow ox and 15 friends to play local tyrants sub-field game, cattle and cows decided to let you divide the field, the landlord's field can be regarded as a rectangle, each position has a value. The method of dividing the field is three knives, divided into 16 parts, as leading cadres, cattle and cows will always choose the total value of the smallest of the field, as the best friend of cattle and cows, you want to get the value of cattle and cattle, and as large as possible, you know the maximum can be how much?
Enter a description:
each input contains 1 test cases. The first line of each test case contains two integers n and m (1 <= N, M <= 75), representing the size of the field, the next n rows, each row containing a number between M and 0-9, representing the value of each block position.
Output Description: The
output line indicates the maximum value the bull can achieve.
Example 1
input
4 4
3332
3233
3332
2323
output
2

Problem Source: https://www.nowcoder.com/practice/fe30a13b5fb84b339cb6cb3f70dca699 Problem Analysis

1° Original Analysis
In this problem we need to try different partitioning schemes and then get the maximum value of the smallest area under all partitioning schemes.

If we follow this simple idea, we need to use the time complexity of n^6 algorithm, under this topic, this time complexity is certainly not acceptable.
(The final solution is not I think out, I borrowed from the cattle network of the vitality of the students to share the code, I did a little optimization on the basis of his code)

2° Conversion Ideas
We think about this in a different way, first of all we may get the calculated value of this whole piece of land (after the land value, we will continue to use the calculation of the value of this argument). The maximum value of our final minimum area must be no greater than one-sixteenth of the calculated value of the entire plot. Then we can try to find an X that allows us to divide the land into 16 regions where the calculated value is not less than X, which is the maximum of all the values we can find.

This is the question of converting the original intuitively n^6 time complexity into log (n) f (n). Log (n) is used to find the time complexity of x using dichotomy, f (n) is the time complexity to confirm that X meets the requirements, and if f (n) is not higher than N^6/log (n), then the algorithm will be better than the brute-force lookup method.

3° Select to find an algorithm that confirms whether X meets the requirements
This still needs to be checked in the way of traversal, we must traverse at least one direction of the choice, such as row. But in another direction we don't have to traverse.
After confirming the division of the line direction, we introduce a conclusion

If the area of our former K-block is small enough, then the best division of the N-k block in this case will not be inferior to that of the former K-block when the area is drawn more.

We need to cut three knives in the direction of the row, if the first cut off the calculated value is the smallest, then the remaining two knives cut out the best division, should be at least not inferior to the first knife cut off more areas of the best division. This is simple, of course, may be a bit difficult to understand, can be combined with the code judge () to understand. Code

#include <stdio.h> #include <string.h> const int MAXN = 76;
int AREA[MAXN][MAXN], G[MAXN][MAXN];
Char FIELD[MAXN][MAXN];
int solve ();
int geta (int, int, int, int);
BOOL Judge (int, int, int, int);
BOOL Isareaok (int);
int n, m;
    int main () {scanf ("%d%d", &n, &m);
    for (int i = 0; i<n; scanf ("%s", Field[i]), i++);
printf ("%d\n", Solve ());
    } int Solve () {//Converts a character array into a numeric array int i, J;
    for (i = 1; l <= N; i++) for (j = 1; j <= M; j + +) G[i][j] = field[i-1][j-1]-' 0 ';
    int Max = 0;
    Generates the calculated value of the area that both landscape and portrait contains the first element of the team area[1][1] = g[1][1];
    for (j = 2; J <= M; j + +) Area[1][j] = G[1][j] + area[1][j-1];
    for (i = 2; I <= n; i++) area[i][1] = g[i][1] + area[i-1][1];  Dynamically calculates the value of all areas starting from the upper-left corner for (i = 2; I <= N, i++) for (j = 2; J <= M; j + +) Area[i][j] = G[i][j] +
    AREA[I-1][J] + area[i][j-1]-area[i-1][j-1];

    int L = 0, r = area[n][m]/16+1; Binary traversal, locking of values satisfying the condition while (L + 1<r){int mid = (L + r)/2;
        if (Isareaok (mid)) L = mid;
    else R = Mid;
    }//if (Isareaok (R)) return R;
return l; }//calculation (I,J) to (K,Q) the computed value of the area int geta (int i, int j, int k, int q) {return area[k][q]-area[i-1][q]-area[k][j-1] +
AREA[I-1][J-1]; }//To determine whether the partitioning of a given row can satisfy the requirements bool judge (int i, int j, int k, int x) {//Using traversal method, try to cut into 16 blocks to meet the calculated value >x//first method input has
    Given the constraints on the matrix line, so in the attempt to split the vertical time to take a greedy way, each area is small enough int q, cnt = 0, pre = 0;
        for (q = 1; q <= m; q++) {int x1 = Geta (1, pre + 1, I, q), x2 = Geta (i + 1, pre + 1, J, Q);
        int x3 = Geta (j + 1, pre + 1, K, q), x4 = Geta (k + 1, pre + 1, n, q);
            if (x1 >= x && x2 >= x && x3 >= x && x4 >= x) {pre = q;
            cnt++;
        if (cnt = = 4) break;
}} return CNT = = 4;
    }//Determine if the land can be cut out 16 blocks of bool Isareaok (int x) with a value greater than x {//traverse the row above the split way int I, j, K; for (i = 1; i<n; i++) for (j = i + 1; j<n;
    J + +) for (k = j + 1; k<n; k++) if (judge (I, J, K, X)) return true;
return false; }
Reference

https://www.nowcoder.com/questionTerminal/fe30a13b5fb84b339cb6cb3f70dca699
https://blog.csdn.net/damotiansheng/article/details/52160496

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.