Bzoj 2241: [SDOI2011] hit the ground rat violence

Source: Internet
Author: User
Tags gopher

2241: [SDOI2011] hit Gopher time limit:10 Sec Memory limit:512 MB
submit:749 solved:488
[Submit] [Status] [Discuss] Description

The hamster is a game in which there are some hamster holes on the ground, and the hamsters will come out of the hole from time to time and then retract into the hole. The goal of the player is to hit the head with a hammer when the ground mouse reaches out, and the more points it gets, the higher the score.

m*n , each of which represents a gopher hole, then the hammer can cover r*c all gopher holes in the area. But the modified Hammer has one drawback: every time you wield a hammer, for this r*c , the hammer will knock out exactly one gopher. That is, in the area where the hammer is covered, each burrow must have at least 1 only hamsters, and if the number of hamsters in a burrow is greater than 1 , then this burrow will only have 1 only the mouse is knocked out, so every time you wield a hammer, it happens to have r*c only rats were knocked out. Because the internal structure of the hammer is too precise, you cannot rotate the hammer during the game (that is, you cannot swap r and c ).

You can arbitrarily change the size of the hammer (that is, you can arbitrarily specify R and C sizes), but the work of a modified hammer can only be done in front of the hamster (i.e. you cannot knock off a part of the hamster and then change the hammer's specifications). Your task is to find out if you want to destroy all the hamsters, at least the number of times you need to wield a hammer.

Hint: Since you can set the size of the hammer to 1*1, there is always a solution to the subject.

Input

The first line consists of two positive integers M and N;

The following M lines describe the map in rows of N positive integers, each representing the number of hamsters in the burrow at the corresponding location.

Output

Outputs an integer that represents the minimum number of flapping times.

Sample INPUT3 3

1 2 1

2 4 2

1 2 1

Sample Output
4

"Sample description"

Use 2*2 hammers, waving once at the top left, bottom left, top right, and right.

"Data size and conventions"


For 100% of data, 1<=m,n<=100, other data is not less than 0, not greater than 10^5
HINT

In turn, if we know the hit position, can quickly reverse the board, think of one hit (x, y) (x+r,y+x) plus 1, (x+r,y) (x,y+c) minus 1 with two-dimensional prefix and engage, so we now have a prefix and, restore the original matrix, and then can be violent enumeration R, C Then violence judgment, time complexity constant minimum O (n^4)

Example large pits, output maximum area, the minimum number of times can be a sample TAT

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineMAXN 103#defineUpdate (I,J) if (vis[i][j]!=vistime) tnow[i][j]=tot[i][j],vis[i][j]=vistime;intTOT[MAXN][MAXN];intVis[maxn][maxn],vistime=0;intTNOW[MAXN][MAXN];intMain () {Freopen ("Input.txt","R", stdin); intn,m; scanf ("%d%d",&n,&m); intsum=0;  for(intI=1; i<=n;i++)                 for(intj=1; j<=m;j++) scanf ("%d", &tot[i][j]), sum+=Tot[i][j];  for(inti=n+1; i>=1; i--)                 for(intj=m+1; j>=1; j--) Tot[i][j]-=tot[i-1][j]+tot[i][j-1]-tot[i-1][j-1]; BOOLFlag; intans=0;  for(intR=1; r<=n;r++)        {                 for(intC=1; c<=m;c++)                {                        ++Vistime; Flag=true; if(R*c<=ans)Continue; inti,j;  for(i=1; i+r-1<=n;i++)                        {                                 for(j=1; j+c-1<=m;j++) {update (I,J); Update (i+r,j); Update (I,j+c); Update (i+r,j+c); if(tnow[i][j]<0) {flag=false;  Break; } tnow[i+r][j]+=Tnow[i][j]; Tnow[i][j+c]+=Tnow[i][j]; Tnow[i+r][j+c]-=Tnow[i][j]; TNOW[I][J]=0; }                                if(!flag) Break;  for(; j<=m;j++) {update (I,J); if(Tnow[i][j]) {flag=false;  Break; }                                }                                if(!flag) Break; }                         for(; i<=n;i++)                        {                                 for(j=1; j<=m;j++) {update (I,J); if(Tnow[i][j]) {flag=false;  Break; }                                }                                if(!flag) Break; }                        if(flag) {ans=r*C; }}} printf ("%d\n", sum/ans);}

Bzoj 2241: [SDOI2011] hit the ground rat violence

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.