[Bzoj] 2321: [beijing2011 training] star machine (Mathematics + special skills)

Source: Internet
Author: User

 

I can't think of it ..

The first thought was an explosive search, and the data range was abandoned. The second thought it was a network stream (because only row and column operations were performed, we set the starting point to the key edge, set the capacity, and other troublesome things to run the largest stream .)

Mathematics...

First, this question is personalized:

The answer has nothing to do with the moving method (because there are row and column restrictions and there are two moving points in the same column in the same row at the same time, so long as there is a vertex in this row that is the end point, no matter how it accumulates, the distance between the reachable vertex and here is equal)

This allows us to bypass the answer.

Let's look at the rectangle.

0 0 0 1

0 0 0 0

1 0 0 0

We need to go from the lower left corner to the upper right corner (the lower left corner of this question must arrive at the upper left or lower right corner, and then to the upper right corner)

The distance is the same .. That is, the merge answer is the same.

In a rectangle, the distance between the diagonal lines does not change...

In addition, we need to fully explore its nature.

We put this rectangle into each vertex. the upper left corner of the original rectangle is the matrix of each vertex.

 

Then there is an online question:

First, let us assume that two vertices (I, j), (I, K) move one cell to the center, and K> J + 1, then we can obtain the value K-J, in this way, we define the energy of each star at each vertex as a [(I, j)] = I * I + J * J, in this way, the energy starting from these two points is I * I + J * j + I + K * K. After moving, the two points become (I, j + 1 ), (I, k-1), when the energy is I * I + (J + 1) * (J + 1) + I * I + (k-1 ), at this time the energy difference is 2 * K-2 * J, in order to obtain the value of 2 times, because for all values can be obtained using this method, therefore, we can calculate the energy and the energy of the initial situation, and subtract> 1 is the answer.

The reason for doing so is what I mentioned above.

 

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }int main() {int ans=0, n, m;read(n); read(m);for1(i, 1, n) for1(j, 1, m) ans+=(i*i+j*j)*getint();for1(i, 1, n) for1(j, 1, m) ans-=(i*i+j*j)*getint();print(ans>>1);return 0;}

 

 

 

 

 

Description

The time on magic land has passed several centuries ......

Now, people talk about a Legend: in the past, their ancestors came to an island in the east, where it was simply another world. People on magic land, good at analysis and construction, always don't understand how people there drive and manipulate magic without precise experiments and computation.

By accident, a magician came to magic land and left a magic box, casket of star, on its way ).

Although I don't know what the box is, after a lot of experiments and calculations, people have already understood some of its facts:

1. there are N x m areas in the star, which can be seen as grids divided into N rows and M columns. Each area has several units of objects called "stars, the minimum unit of this object has been determined. Therefore, this number is always an integer.

2. magic allows you to drive one unit of "star" in each of the two areas of the star device in the same row or column that are not adjacent (the area with a public edge is called adjacent ", so that they move 1 cell to the center respectively.

3. Every time you use the method in step 2 to drive the "Star", it will produce magic, and magic will get this magic. The magic volume is equal to the number of zones separated between the two regions.

In this way, we can use an n × m number table to represent the star state, such as N = 2, M = 3:

2

0

1

   

1

2

0

5

1

4

   

5

1

4

When the star is in the left graph, it changes to the State shown in the right graph by manipulating the "star" (corresponding to the area of the bold number) in the 1st and 3 regions of the first row, at the same time, the magic of one unit is generated (because the two regions are exactly separated by one region ).

After further research, people know the initial state (INI) of the star and the final state (FIN) of the star ).

You want to know how much magic a star provides to its owner. That is, after a series of above operations from the first State (INI) to the final state (FIN), at most how much magic is produced.

Note that the number of "Stars" in each region cannot be negative during the operation, that is, if there is no "Stars" in that region, of course, you cannot continue the operation.

 

Input

The first line contains two positive integers, N and M, indicating the size of the star.

The next n rows contain m natural numbers: iniij, which depicts the first State (INI ).

In the N rows after an empty row, each row contains m natural numbers: finij, which depicts the final state (FIN ).

 

Output

Output a positive integer, indicating the magic at most.

Sample input [input Example 1]
5 5

1 0 0 0 1
0 0 0 0 0
0 0 0 0 0
0 1 0 1 1
1 0 0 0 0

0 0 0 0 0
0 0 0 0 1
2 0 0 0 1
0 0 2 0 0
0 0 0 0 0


[Example 2]
1 4
10 20 30 40
0 0 100 0
Sample output [Output Example 1]
7

[Example 1]
The only operation method is:
Perform one operation on two "Stars" in the 5th columns to create magic 2;
Perform Two operations on two "Stars" in the 1st columns to create 3 + 1 magic;
Perform one operation on the two "Stars" in the second row to create magic 1;

A total of seven magic units are created.

[Output Example 2]
50

Hint

[Data scale and Conventions]

N ≤ 2 in 40% of the data, as shown in example 2;

1 ≤ n, m ≤ 100%, iniij, and finij ≤ 200 of 1000 of the data.

All data ensures that there is at least one operation method for the star to change from the initial state to the final state, and that the initial state and the final state are not exactly the same.


Source

 

 

[Bzoj] 2321: [beijing2011 training] star machine (Mathematics + special skills)

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.