Separation Program

Separation Program

Inspection point: half the answer

First a word: Recently always doing the half and then answer the questions that have no idea even half can not be seen. . . The insights in this regard to exercise ah!

Face questions

As we all know, the owner of Z small enough to destroy the power of the world, but he can not control the strength of this relationship, small small J and Z are very close one day a small J premonition that the power of small Z body would break out.

The outbreak of force must be stronger than ever, so that the small overall Z is divided into two, with each other against the gravitation toward each other, once the integration of the world will cease to exist.

In order to save the world, J decided to build a small vessel G, the two parts of the small Z respectively mounted in one part of the vessel G, to control small Z.

Magic crystal composition from the container, they form a matrix of rows and columns, each crystal has its own magic energy value, the container needs to be divided into two parts, that each magic crystal belongs to one and only one portion, and any a magic crystal can be only through the path and they belong to the same part of the magic crystal to change direction more than once by the arrival of another one and he was in the same part of the magic crystal in the matrix.

The following example is not legal container 2, the container 3 is legal.

265431584.JPG

For each part, he does not belong to poor stability of the energy value of all crystal magic of this section (max - min). For the entire container, the maximum value of instability is the instability of the two parts.

In order to know he could not save the world, not a waste of time, small J wanted to know the smallest unstable value of the entire container.

Input Format

The first two rows of the matrix size number of representatives magic crystal composition.

Then rows of integer values ​​represent the energy of magic crystal.

Output Format

Line an integer value representing the minimum instability.


Because the title says " minimum maximum two very poor ", so obviously nature has half the answer. It is easy to think that we are asking for very poor extremes is the whole matrix minus the smallest maximum, minimum and poor is 0, it is possible to (0, Max_all-Min_all) interval divided by two, probably 30 times.

Since the constant upper brought constant log 30, we need to check the binary values ​​are legal in the mid time complexity O (n * m) of.

How to do it

First, if the graphic is legal, it will not appear concave or convex. For a crystal portion in each row must have appeared from the same side of the starting position of each row and the number of blocks are unified crystal layer of greater or less than the number of blocks (the number in each row or single down monocytogenes) .

Next, know the graph structure, it began to check the legality of mid value. Every poor very slow in the area considered an uncertain, but if the entire rectangle contains the maximum value in an area, then only to see that a minimum number on the line when calculating the poor. Similarly, the minimum value of the whole rectangle as well.

So, we get a mid value, we will assume to find qualified two, and one each on the left with respect to the row number of the row of blocks monotonically decreasing, in the left block and the global maximum, global minimum in the right block . The present discussion is provided to the i-th row and j the number, and this number belongs to a left block, a record Last, the line represents the farthest reach of the requirements are satisfied when the number of Last. Obviously, j <= Last.
Now we ask, if the answer to mid, then the maximum value of the left block (that is, the global maximum) minus any one of a [i] [j] need <= mid, so enumerate each line can be found on the left block boundaries of each row .

For the right block, because we have dealt with the boundaries of the left block, then the rest is not that exactly the right piece of it? Then for the right number of blocks, the method using the left block and a similar process. Now we ask if the answer mid, then any one of a right block [i] [j] by subtracting the minimum value of the right block (i.e. global minimum) needs <= mid. If there is any one right block box does not satisfy this condition, then it should be mid transfer large. Finally, if the entire matrix of conditions are met, then consider the mid adjust small. (Dichotomous answer)

Thus, you can check the answer to the (n ^ 2) time complexity in a relaxed O.

However, we consider only about two, one for each row on the left with respect to the line monotonically decreasing number of blocks, the global maximum and the minimum value in the case of the left block, right block in global. Missed otherwise. In fact, it is not difficult, it put the whole matrix mirror, running four half answer, you can find all situation.

Code:

#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define reint register int
#define relong register long long
#define intinf 1e9
#define llinf 1e17
using namespace std;

char buf[1<<20],*p1,*p2;
#define GC (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?0:*p1++)
//#define GC getchar()
inline ll read_64(){ll X=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=GC;} while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=GC;return w?-X:X;}
inline int read_32(){int X=0,w=0;char ch=0; while(!isdigit(ch)){w|=ch=='-';ch=GC;} while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=GC;return w?-X:X;}

const int maxn=2010;
int a[maxn][maxn],ma=-intinf,mi=intinf,ans=intinf,n,m;

void spin1()//左右镜像 
{
    for(reint i=1;i<=n/2;i++)
      for(reint j=1;j<=m;j++)
      {
        swap(a[i][j],a[n-i+1][j]);
          }
}

void spin2()//上下镜像 
{
    for(reint i=1;i<=n;i++)
      for(reint j=1;j<=m/2;j++)
      {
        swap(a[i][j],a[i][m-j+1]);
      }
}

bool check(int x)
{
    int Last=m;
    for(reint i=1;i<=n;i++)
    {
        for(reint j=1;j<=Last;j++)
          if(ma-a[i][j]>x)
          {
            Last=j-1;break;
          }
        for(reint j=Last+1;j<=m;j++)
          if(a[i][j]-mi>x)
            return false;
    }
    return true;
}

void binary_search(int l,int r)
{
    int mid;
    while(l<=r)
    {
        mid=((l+r)>>1);
        if(check(mid)) r=mid-1;
        else l=mid+1;
    }
    while(check(l)) l--;
    l++; 
    ans=min(ans,l);
}

int main()
{
    n=read_32();m=read_32();
    for(reint i=1;i<=n;i++)
    {
        for(reint j=1;j<=m;j++)
        {
            a[i][j]=read_32();
            mi=min(mi,a[i][j]);
            ma=max(ma,a[i][j]);
                }
    }
    binary_search(0,ma-mi);
    spin1();
    binary_search(0,ma-mi);
    spin2();
    binary_search(0,ma-mi);
    spin1();
    binary_search(0,ma-mi);
    printf("%d",ans);
    return 0;
}
/*
4 4
1 12 6 11
11 4 2 14
10 1 9 20
4 17 13 10
*/

In fact, for each check out, we can first (n ^ 2) O pretreatment each line the minimum value of the prefix, the most minimum array, in which each mid determines whether the binary search method, can be O (n ^ 2 ) checks down to O (nlogn).But I was too weak to fight it out

Guess you like

Origin www.cnblogs.com/playerzmr/p/11802755.html