"Bzoj 1048" [HAOI2007] Split matrix

Source: Internet
Author: User

1048: [HAOI2007] Split matrix time limit: ten Sec Memory Limit: 162 MB
Submit: 498 Solved: 362
[Submit] [Status] Description

A a*b digital matrix is segmented as follows: The original matrix is divided into two matrices along a line, and then the resulting two matrices continue to be so segmented (or, of course, only one of them), so that the original matrix is divided into n matrices after splitting (n-1). (Each partition can only follow the gap between the numbers) there is a score in each position of the original matrix, and the total score of a matrix is the sum of the points in each position contained in it. Now we need to divide the matrix into n matrices according to the above rules, and minimize the mean variance of the total score of each matrix. Please program the given matrix and N to find the minimum value of the mean variance.

Input

The first behavior is 3 integers, representing a,b,n (1

Output

Only one number, which is the minimum value of the mean variance (rounded to 2 digits after the decimal point)

Sample Input5 4 4
2 3 4 6
5 7 5 1
10 4 0 5
2 0 2 3
4 1 1 1Sample Output0.50


In order to facilitate the calculation of the average variance of the formula to expand the simplification, no matter how to divide the average is unchanged.


The original formula becomes (x1^2+x2^2+...+xn^2)/n-(tot/n) ^2


So the sum of the squares of x is to be minimized.


F[x1][y1][x2][y2][k] Means (x1,y1) as the upper-left corner, (x2,y2) is the lower right corner, the matrix is divided into K-part of the minimum sum of squares.


Then we enumerate the cutting position (horizontal, vertical), and then enumerate the cut two pieces of the block number (1~k-1), with the memory of the search to do it.


#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath > #include <algorithm> #define INF 0x3f3f3f3fusing namespace Std;int f[15][15][15][15][15],n,pre[15][15],x[15 ][15],a,b;bool OK (int x1,int y1,int x2,int y2,int k) {if ((x2-x1+1) * (y2-y1+1) >=k) return True;return false; int cal (int x1,int y1,int x2,int y2) {return (pre[x2][y2]-pre[x1-1][y2]-pre[x2][y1-1]+pre[x1-1][y1-1]);} int dp (int x1,int y1,int x2,int y2,int k) {if (f[x1][y1][x2][y2][k]!=-1) return f[x1][y1][x2][y2][k];if (k==1) {f[x1][y1][ X2][y2][k]=cal (x1,y1,x2,y2) *cal (x1,y1,x2,y2); return f[x1][y1][x2][y2][k];} int minn=inf;//verticalfor (int i=y1+1;i<=y2;i++) for (int j=1;j<k;j++) {int Ma=max (J,K-J), Mi=min (J,K-J), if (OK ( X1,Y1,X2,I-1,MA) Minn=min (MINN,DP (X1,y1,x2,i-1,ma) +DP (X1,I,X2,Y2,MI)), if (OK (x1,i,x2,y2,ma)) minn=min (MINN,DP (x1    , Y1,x2,i-1,mi) +DP (X1,i,x2,y2,ma)); }//horizontalfor (int i=x1+1;i<=x2;i++) for (int j=1;j<k;j++) {int Ma=max (J,K-J), Mi=min (J,K-J);    if (OK (x1,y1,i-1,y2,ma)) Minn=min (MINN,DP (X1,y1,i-1,y2,ma) +DP (I,y1,x2,y2,mi));    if (OK (i,y1,x2,y2,ma)) Minn=min (MINN,DP (I,y1,x2,y2,ma) +DP (X1,y1,i-1,y2,mi)); }return F[x1][y1][x2][y2][k]=minn;} int main () {scanf ("%d%d%d", &a,&b,&n), for (Int. i=1;i<=a;i++) for (int j=1;j<=b;j++) scanf ("%d",& X[I][J]), pre[i][j]=pre[i][j-1]+x[i][j];for (int i=2;i<=a;i++) for (int j=1;j<=b;j++) PRE[I][J]+=PRE[I-1][J]; memset (F,-1,sizeof (f));dp(1,1,a,b,n);d ouble ans=0.0;ans= (double) f[1][1][a][b][n]/(double) n-(double) (pre[a][b]* PRE[A][B])/(double) (n*n);p rintf ("%.2lf\n", sqrt (ans)); return 0;}


In fact, this problem at the beginning WA no several times.


I used to be a poj1191.

Checkerboard Segmentation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12647 Accepted: 4481

Description

A 8*8 board is split as follows: The original board is cut down a rectangular checkerboard and the rest is also a rectangle, and then the remainder continues to be so segmented, so that after cutting (n-1), together with the last remaining rectangular checkerboard there is a total of N-block rectangular chessboard. (Each cut can only be performed along the edge of the checkerboard lattice)

Each lattice on the original board has a score, and the total score of a rectangular checkerboard is the sum of the scores of the squares it contains. Now it is necessary to divide the chessboard into N-block rectangular chessboard according to the above rules, and to minimize the average variance of the total score of each rectangular checkerboard.
Mean variance, where average, Xi is the total score of the block I rectangular checkerboard.
Please program the given checkerboard and N to find the minimum value of O '.

Input

The 1th Act is an integer n (1 < n < 15).
Lines 2nd through 9th each Act 8 non-negative integers less than 100, indicating the score of the corresponding lattice on the board. Each row is separated by a space between two adjacent numbers.

Output

Only one number is O ' (rounded to three digits after the decimal point).

Sample Input

31 1 1 1 1 1 1 31 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 01 0 3

Sample Output

1.633

And this problem is super like.


But if you look closely you will find the difference.


POJ This problem after cutting a knife, can only be divided into one piece!! And Bzoj this way can be divided into two pieces!!


So POJ does not need to enumerate the number of blocks that are split. One of them is 1, the other is k-1!!.

"Bzoj 1048" [HAOI2007] Split matrix

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.