Description
Description
In a distant country, one side is a beautiful lake with great scenery, and the other side is a desert without any limitations. The country's administrative divisions are very special. They form a rectangle with N rows and M columns, as shown in. Each grid represents a city and each city has an altitude. Water conservancy facilities are now needed in some cities to allow residents to drink clear water as much as possible. There are two types of water conservancy facilities: water storage plants and water stations. The function of a water storage plant is to use a water pump to extract water from a lake to a reservoir in a city. Therefore, water storage plants can be built in only 1st cities adjacent to lakes. The function of the water delivery station is to use the height gap through the water transmission line to transport the lake water from a height to a low place. Therefore, the premise that a city can build a water supply station is that there are adjacent cities with a higher altitude and a public edge, and water conservancy facilities have been built. Because the city in line N is near the desert, it is the arid area of the country, so each city must have water conservancy facilities. So can this requirement be met? If yes, calculate the minimum number of water storage plants that can be built. If not, find the number of cities that cannot be built with water conservancy facilities in dry and dry regions.
Input description
Input description
Two numbers in each input line are separated by a space. The first line of the input is two positive integers n and M, indicating the size of the rectangle. Next n rows, each row has M positive integers, which represent the altitude of each city in turn.
Output description
Output description
The output has two rows. If the requirements are met, the first line of output is integer 1, and the second line is an integer, which indicates that at least several water storage plants are built. If the requirements cannot be met, the first line of the output is an integer of 0, and the second line is an integer, which indicates that water conservancy facilities cannot be built in cities in several dry regions.
Sample Input
Sample Input
2 5
9 1 5 4 3
8 7 6 1 2
Sample output
Sample output
1
1
Data range and prompt
Data size & hint
[Data Scope] This question contains a total of 10 test data. The scope of each data is shown in the following table: test data number can meet requirements n M 1 cannot ≤ 10 ≤ 10 2 cannot ≤ 100 ≤ 100 3 cannot ≤ 500 ≤ 500 4 can = 1 ≤ 10 5 can ≤ 10 ≤ 10 ≤ 10 6 can ≤ 100 ≤ 20 7 ≤ 100 ≤ 50 8 ≤ 100 ≤ 100 9 ≤ 200 ≤ 200 ≤ 500 10 ≤ 500 ≤ for all 10 data, the altitude of each city cannot exceed 10 ^ 6
Example 2
Data range
At the beginning, I considered this question as a particularly difficult question. Later I found myself thinking too much.
After that, I got a wrong understanding of the meaning of the question, and then I took 40 points in less than an hour, 30 points because he was not able to get full, and the other 10 points, it is probably good luck.
After reading the question, I found myself tooyoungtoosimple, and then I decided to do it again. As for the first question of this question, we only need to build equipment for coastal cities that cannot transport water from other cities, and then see if we can supply desert cities with water. Then the first question is solved, but what about the second question?
When I asked the second question, I first thought of a question in the original black book, which seems to be called a sprinkler device. The question is that there is a large line with many small lines on it, take as few small line segments as possible, so that each point of the entire large line segment is in one or more selected small line segments.
Then, this question can be used to understand coastal cities as line segments and correspond to desert cities respectively. But is this true?
I thought for half an hour and didn't want to understand it. I looked at the problem and said it was right.
Then I instantly discovered that this was correct, and then I proved it myself. (Why didn't I do it myself)
Assume that a coastal city does not form a continuous segment of several cities, then it can form a triangle with the cities in the leftmost and rightmost sections of the city it controls, the cities in the middle of the two cities on the bottom edge (desert) are not controlled by the coastal cities. First, we have come to know that all desert cities will be controlled, so the cities in the middle will be controlled by other coastal cities. Then we connect the desert city to the coastal city. You will be surprised to find that this side passes through the triangle. According to the question, this side is separated from the intersection of the triangle. The part close to the sea must be higher than the part close to the desert, the side of the triangle with which it is located has the same nature. That is to say, the inbound water in both directions can go through the intersection and then enter the city inside the triangle. That is to say, A desert city controlled by a coastal city must be continuous.
Then this question is transformed into a question that I have mentioned, and can be solved with simple greed.
1 var 2 A: array [0 .. 501, 0 .. 501] of longint; 3 F: array [0 .. 501, 0 .. 501] of Boolean; 4 n, m, I, J, K, T, P, ANS, Max, S: longint; 5 head, tail: array [1 .. 500] of longint; 6 ff: array [1 .. 500] of Boolean; // store a city without going through 7 8 Procedure DFS (X, Y: longint ); // traverse 9 begin10 if x = n then11 begin12 if y
10 noip traffic diversion solution report