[Question background]
Xiao Qi always thinks strange questions in mathematics.
[Problem description]
Given a matrix of N * m, each element in the matrix AIJ is a positive integer.
The following provisions
1. valid paths start from the upper left corner of the matrix and can only go to the right or down at a time. The destination point is the lower right corner.
2. The elements in the N + M-1 lattice passing through the path are A1, A2... A (n + m-1), aavg is the average flat of AI, and the V value of the path is (n + s-1) * Σ (AI-aavg) ^ 2 (1 <= I <= N + s-1)
Find the legal path with the smallest value of V, and output the value of V. There are multiple groups of test data.
[Input format]
The first line contains a positive integer T, indicating the number of data groups.
For each data group: the first row contains two positive integers n and M, indicating the number of rows and columns in the matrix.
Next n rows, each row has M positive integers AIJ, which describe this matrix.
[Output format]
For each query, a row of an integer is output to indicate the required results.
[Example input]
12 2 1 2 3 4
[Sample output]
14
[Data Scope]
For 30% of data, n <= 10, m <= 10
There are another 40% of Data n <= 15 m <= 15, and the element in the matrix is not greater than 5
For 100% of data t <= 5, n <= 30, m <= 30, the elements in the matrix are not greater than 30.
All of the following are your own words
It's another question in the little odd series. I feel like it's going to be brainwashed by the cute little odd. (Please be a kitten and never think about it again in your math class)
First, let's take a look at this mysterious style. oier with a junior high school degree or above scream in their hearts.: I know! This is the variance!
But why is the final result still an integer while the average number is required ?? Are you sure you want to erase all the decimal points ?? Will it explode ?? Noip does not seem to allow the use of long double...
Don't worry. Let's take a look at the data scale first. It's not very big and we're familiar with it.Dynamic PlanningYou should think of DP directly. But -- I have to go to the end to know what the average is. This aftereffect blocks myAC path..
So let's go back to the formula and consider simplification.
We can see that at the beginning of the formula, we multiply one (n + s-1). After simplification, we actually eliminated all the denominator (!)
This is probably the final simplification.
(N + m-1) * (A1 ^ 2 + A2 ^ 2 + A3 ^ 2 +... + AI ^ 2)-(A1 + A2 + A3 +... + AI) ^ 2
Aha, there was no double type at all, and the results were all integers.
Now let's take a look at how to write the DP equation. How do we express the state of each coordinate ??
......
......
......
......
......
F [I] [J] [...?
......
......
The same sigma (AI) can represent different sigma (AI ^ 2 (!)
So our arrayF [I] [J] [k]The first two dimensions represent the position (I, j), and K represents the SIGMA (AI) (that is, the part after the formula just removes the square ), in this state, the first half of the formula (n + s-1) * (A1 ^ 2 + A2 ^ 2 + A3 ^ 2 +... + AI ^ 2) Minimum value (!) That's great. In this way, it can be transferred, satisfying both the nature of the optimal sub-structure and the non-aftereffect.
I believe everyone can write the transfer equation on their own. (If you can't do it, check the code)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 7 #define For(i,a,b) for(register int i=a;i<=b;++i) 8 #define Dwn(i,a,b) for(register int i=a;i>=b;--i) 9 #define Re register10 #define Pn putchar(‘\n‘)11 #define llg long long12 using namespace std;13 const int N=32;14 int n,m,a[N][N],nxm,Mx=0;15 llg f[N][N][2000];16 inline void read(int &v){17 v=0;18 char c=getchar();19 while(c<‘0‘||c>‘9‘)c=getchar();20 while(c>=‘0‘&&c<=‘9‘)v=v*10+c-‘0‘,c=getchar();21 }22 void write(llg x){23 if(x>9)write(x/10);24 int xx=x%10;25 putchar(xx+‘0‘);26 }27 int main(){28 freopen("matrix.in","r",stdin);29 freopen("matrix.out","w",stdout);30 int T; read(T);31 while(T--){32 read(n); read(m);33 nxm=n+m-1;34 For(i,1,n) For(j,1,m)read(a[i][j]);35 36 memset(f,-1,sizeof(f));37 38 f[1][1][a[1][1]]=nxm*a[1][1]*a[1][1];39 Mx=a[1][1];40 41 For(i,1,n) For(j,1,m){42 if(i==1&&j==1)continue;43 int adx=a[i][j];44 int pMx=Mx;45 Dwn(k,pMx,1){46 llg fx;47 48 fx=f[i-1][j][k]; // I walk to you from your top side49 if(fx!=-1){50 Mx=max(Mx,k+adx);51 52 if(f[i][j][k+adx]==-1){53 f[i][j][k+adx]=fx+nxm*adx*adx;54 }else{55 f[i][j][k+adx]=min(f[i][j][k+adx],fx+nxm*adx*adx);56 }57 }58 59 fx=f[i][j-1][k]; // I walk to you from your left side60 if(fx!=-1){61 Mx=max(Mx,k+adx);62 63 if(f[i][j][k+adx]==-1){64 f[i][j][k+adx]=fx+nxm*adx*adx;65 }else{66 f[i][j][k+adx]=min(f[i][j][k+adx],fx+nxm*adx*adx);67 }68 }69 70 }71 72 }73 74 llg ans=1e18;75 For(i,1,Mx) if(f[n][m][i]!=-1){76 ans=min(ans,f[n][m][i]-i*i);77 }78 write(ans); Pn;79 }80 fclose(stdin); fclose(stdout);81 return 0;82 }
Hope to get support (recommended)
Matrix of Xiaoqi (Dynamic Planning