12646. ORCHARD Constraints
Time limit:3 secs, Memory limit:256 MB
Description
Alex and Bert are the had been working for many years in a big orchard of their uncle where they planted trees. The orchard is arranged as a array of size n by m of trees. Alex had been planting apple trees and Bert had been planting banana trees; However, the were not systematic and so apple trees stand among banana trees and vice versa. Each of them have planted at least one tree.
When coming-to-retirement, the uncle decided to officially transfer the ownership of the "The Trees to the". The uncle informed the "the" he'll first pass the orchard to Alex. Next, Alex and Bert can cut out an rectangular area from the orchard, and the ownerships of all trees in the rectangular AR EA is to is transferred to Bert. All further adjustments of the splitting had to is done with a lawyer.
Alex would like-to-keep all apple trees and Bert would like-to-keep all-banana-trees, but does not want-replant any tree. When they talked to a lawyer, the lawyer informed them so the ownership of a tree can be transferred from one owner to Another, but the lawyer would charge $ to transfer the ownership of a tree. Therefore The starting rectangle such that the legal fees is as low as possible.
The following figures show three examples, where 0 and 1 indicates an apple and banana tree respectively.
In the first example, the best of the ' to ' cut out of the fourth column and to assign it-Bert, as indicated by the Rectangul Ar outline. Afterwards, there is and trees that is out of place, and their ownerships has to be transferred–one banana tree from Alex to Bert and one apple tree from Bert to Alex. Hence, the fees is.
In the second example, the best-of-the-is-to-cut out trees that was not at the border of the field and then to transfer the O Wnership of six trees. The fees is $6.
In the third example, the best of the ' to ' cut out of the rectangle consisting of 3 banana trees, and then to transfer the Owne Rship of the rightmost banana tree to Bert. The fee is $. Note that there was an alternative-on-a-costs.
Input
The first line of the input is the positive numbers n and M (1<=n<=150, 2<=n*m<=750000) indicating the OR Chard ' s size. Then follow n lines each consisting of m numbers, where 0 represents an apple tree and 1 represents a banana tree.
Output
Must write to the standard output a number, which is the smallest fee required.
Sample Input
5 70 0 1 0 0 1 00 1 1 1 1 1 00 1 1 0 0 1 00 1 1 1 1 1 00 0 1 0 0 1 0
Sample Output
6
Problem Source
2014 every Monday match 13th game
Test instructions: Give the n*m 01 matrix, you choose a x*y matrix, F = x*y inside the number of white dots + not x*y the number of black dots
We're asking for the F-min.
Ideas: F=a+len-b, A is the number of x*y inside 0 points, B is the number of x*y inside 1 points
Just ask A-B to be the smallest.
We enumerate the rows of the selected matrix, and then O (m) asks the answer
This probably wants maximum continuity and the same, if the previous contribution to the back, add
No, 0.
//problem#: 12646//submission#: 3287759//The source code is licensed under Creative Commons attribution-noncommercial-sharealike 3.0 Unported License//URI:http://creativecommons.org/licenses/by-nc-sa/3.0///All Copyright reserved by informatic Lab of Sun Yat-sen University#include <stdio.h>#include<math.h>#include<string.h>#include<iostream>#include<vector>#defineLL Long Long#defineMAXN 1000010#defineINF 0x3f3f3f3f#defineMoD 1000000007using namespacestd;intsum[750010];vector<int>a[ the];intAns1,ans2;voidGetans (intLintm) { intsum1=0, sum2=0; for(inti =1; I <= m; i++) {sum2+=Sum[i]; Sum1+ = LSum[i]; if(sum1>=sum2) {sum1=sum2=0; Continue ; } if(sum2-sum1>ans2-ans1) {ans1=sum1; Ans2=sum2; } }}intMain () {intI,n,m,j,max; intLen, K; while(SCANF ("%d%d", &n,&m)! =EOF) {Len=0; for(i =1; I <= N; i++) {a[i].clear (); A[i].push_back (1); for(j =1; J <= m;j++) {scanf ("%d",&k); if(k==1) len++; A[i].push_back (k); }} ans1=ans2=0; for(i =1; I <= N; i++) { for(k =1; k <= m;k++) sum[k]=0; for(j = i; J <= N; j + +) { for(k =1; k <= m;k++) Sum[k]+=A[j][k]; Getans (J-i+1, M); } } //cout<<ans1<< "" <<ans2<<endl;cout<<ans1+len-ans2<<Endl; } return 0 ;}
View Code
SoJ 12646. ORCHARD