"Csu_bmw officially set up commemorative competition" solution report

Source: Internet
Author: User

 

Limited to your own level, you may have many questions, but you may still have such shortcomings. Some questions are not as many as some "hit questions" as they are after the competition ", I also hope you will forgive O (∩ _ ∩) O ~.

My questions are A, B, C, D, E, and G. Let's share some of my ideas with you.


Problem:A simple problem 

I thought of this question on my way to the dining room, but it was also the most serious question of "hit questions. This topic has a trick, that is, when determining whether the difference between the maximum and minimum values is greater than K, if the int type is used for subtraction, it may be beyond the int expression range, therefore, it can be forcibly converted to long int before comparison.

First, the simplest O (N ^ 2) method is as follows: enumerate each vertex as the start point of the interval, scan to the right, and update the maximum and minimum values of the start point, until the difference between the maximum and minimum values is greater than K.

However, when the interval is relatively long, the calculation workload will be large, and most of them are repeated calculations. If I is fixed as the left endpoint, when enumeration to the right endpoint J, the maximum value and the minimum value is greater than K, then the value of the inevitable J position is greater than the maximum value of [I, J-1] and the difference between the minimum value and the minimum value is greater than K, or smaller than the minimum value and less than-K than the maximum value.

We may only discuss the situation where the J position value is greater than the maximum value and the difference between the minimum value and the minimum value is greater than K. When some vertices on the Right of enumeration I are left endpoints, such as I + 1, if the minimum value of [I + 1, J] is not changed, then no more solutions will be obtained, that is to say, we keep enumerating I + 1, I + 2 ...... It is not until the minimum value in the range [I + X, J] (x> = 1) has changed that a better solution can be obtained.

So we can get a better way. At the beginning, I and J are both at the zero position, and J moves forward until [I, j] the difference between the maximum and minimum values is greater than K, and then I is moved forward until the difference between the maximum and minimum values in [I, j] is not greater than K, and then repeated.

This seems like an O (n) method, but there is still another operation that is not solved, that is, how can we quickly learn the maximum and minimum values in the range of [I, j] at any time? From the perspective of insertion and deletion, you can use a maximum heap and a minimum heap. In this way, each element can be added to a maximum of two heaps each time, and two stacks can be created each time, the overall complexity is O (nlogn. However, in fact, the maximum and minimum values in the range of [I, j] can also be maintained through two monotonous queues. As a result, each element can be entered at most once into two queues, and each of the two queues can be output once, the overall complexity is O (n.

 

Problem B:Covered by a square

During the communication, an acmer told me that this question is similar to 2482 of poj. I did it after reading it, but it is easier than that of poj. If I remember correctly, this question should have been raised when I was lying in bed.

Because the square is arbitrary, there will be a lot of positions, so we should try to "constrain" the position of the square without affecting the result. After thinking about it, we will find that we can get a greedy idea: if it is an optimal solution, there must be a square so that one or more points can be placed on the left side. If there is no point on the left side, we can translate the square to the right without making the solution worse.

In this way, the place of the square is relatively small. Then we will find that if the straight line on the left side of the square is limited, the square can slide up and down, therefore, we can slide the square from the bottom to the top, and constantly count the points in the square to find the optimal solution.

Next we encountered a similar problem. The square sliding is continuous, which makes it hard to determine. But we can use the same greedy idea, each slide will inevitably lead to a point on the side above, so that the scale problem of square sliding will be solved.

So far, the general idea is there, and the rest is the details:

First, how do I determine the positions of the straight lines on the left of a square? Extract the X coordinates to sort the order (or not sort, but after sorting, you can use if to avoid enumerating the same X coordinates as the position of the straight line on the left of the square ), then, enumerate them in sequence.

Second, how can we determine where the square will slide, even if it encounters a point? Because it is slide from the bottom up, if all the original coordinates are sorted by Y, scanning all the points will first encounter the following points, and then encounter the above points, of course, if the X coordinate of a point is not between the straight lines on both sides of the square, you can ignore these points. In this way, a vertex queue can be maintained. The vertices in the queue can be overwritten by the current square. After each vertex is added and deleted, count the number of elements in the queue and update the optimal solution.

 

Problem C:Generating queue

I didn't think of a direct recursive search method at the beginning, so there was no data for this brute force search method, at the beginning, some people used this particular sequence of recursive searches to get rid of the water, and later strengthened the data.

I thought of this question on my way home by train before the Chinese New Year.

Because of the large amount of data, brute-force search cannot solve the problem. A similar idea to brute-force search is the Memory search or recursive DP. After thinking about this, we will find that this question can be converted into a range of DP.

F [I] [J] indicates whether the elements I to J in the queue g can be generated by the first J-I + 1 characters in the queue S, if yes, F [I] [J] is 1; otherwise, F [I] [J] is 0, the state transition equation is f [I] [J] = (F [I + 1] [J] & G [I] = s [J-I]). | (F [I] [J-1] & G [J] = s [J-I]).

 

Problem d:Congruent triangles

The initial idea of question D is to find the length and width of the external rectangle for several forms of the triangle, and then calculate the number of triangles in each form at O (1) time, finally, add them together.

The easy-to-think form is to rotate 90 degrees, 180 degrees, or mirror symmetry, or combine the two, but for some special triangles, there will be more situations.For example, if the triangle is 0 0 5 0 5 5, it is okay when the three points are at 0 0 4 3 1 7, it is also possible when it is located at 0 0 3 4-1 7. Of course, if the coordinate is negative, it can be converted to positive by translation.

At this time, we will think of rotating the triangle around a point to see how many different forms there are. At the same time, we will find a good conclusion that if we do not consider the isosceles triangle, as long as the rotation angle between 0 and degrees is different, the triangle will be different. In addition, you can get all the situations by rotating around one point, and then rotating around other points will produce duplicates. Of course, we also need to consider the rotation of the triangle after the mirror symmetry. Wait for a while and we will discuss it later. First, we will solve the general situation.

We might as well set this triangle to the OAB, where o is the origin, and then rotates the triangle around the O point. Of course, sometimes the coordinates of the points are negative, but it doesn't matter, we only focus on the shape of the triangle and the size of the external rectangle of the triangle, so the coordinate value itself is not important. At this time, if we rotate OA and Ob together, we will find that we need to determine whether points A and B are grid points at the same time, so that the scale of rotation is hard to grasp.

So we may try another way of thinking, first finding out the possible location of the OA, that is, the possible coordinate of the point, and then finding out the possible location of the OB, that is, the possible coordinate of the B point, then, enumerate the location of OA and ob to see if the triangle composed of the enumerated OA and Ob is equal to the given triangle. At first glance, the possible locations of OA and Ob are O (n ).AlgorithmIs O (n ^ 2), but in fact the location of OA is dependent on x ^ 2 + y ^ 2 = n (n represents an arbitrary integer, it is irrelevant to the known amount of the question, according to some theorem, we can know that the number of Integer Solutions for this equation is much smaller than N in the question. Therefore, when we use O (n) Time to pre-process the possible locations of OA and ob, the enumeration and Judgment time is negligible, therefore, the overall complexity of the algorithm is O (n.

After analysis, we can find that the enumerated positions are repeated once. Therefore, we can divide the calculated results by 2. Because the lattice triangle does not exist in an equi-edge triangle, you do not need to consider the Equi-edge triangle.

Due to the time, this question has not been tested too strictly, so I do not dare to guarantee that there will be no loopholes in doing so, so I willCodeI posted it here. If you find any bugs, I hope you can help me identify them.

 

View code

# Include <stdio. h>
# Include < String . H>
# Include <math. h>
# Define Maxd 100010
Struct Point
{
Int X, Y;
} Vec1 [ 4 * Maxd], vec2 [ 4 * Maxd];
Int N, m;
Long Long Int Det (Int X1, Int Y1, Int X2, Int Y2)
{
Return ( Long Long Int ) X1 * Y2 -( Long Long Int ) X2 * Y1;
}
Long Long Int Mul ( Int X1, Int Y1, Int X2, Int Y2)
{
Return ( Long Long Int ) X1 * X2 + ( Long Long Int ) Y1 * Y2;
}
Long Long Int Sqr ( Int X)
{
Return ( Long Long Int ) X * X;
}
Long Long Int Labs ( Long Long Int X)
{
Return X < 0 ? -X: X;
}
Int Getmin ( Int X, Int Y, Int Z)
{
Int T = Y <Z? Y: Z;
Return X <t? X: T;
}
Int Getmax ( Int X,Int Y, Int Z)
{
Int T = Y> Z? Y: Z;
Return X> T? X: T;
}
Void Prepare (point * P, Int X0, Int Y0, Int & N)
{
Int I, J, K;
N = 0 ;
Long Long Int A, B, mode = sqr (x0) + sqr (y0 );
K = ( Int ) SQRT (( Double ) Mode + 0.5 );
For (A = 0 ; A <= K; A ++)
{
B = ( Long Long Int ) SQRT (Mode-A * A +0.5 );
If (A * A + B * B = mode & B! = 0 )
{
P [N]. x = B, P [N]. Y =;
++ N;
P [N]. x =-A, P [N]. Y = B;
++ N;
P [N]. x =-B, P [N]. Y =-;
++ N;
P [N]. x = A, P [N]. Y =-B;
++ N;
}
}
}
Void Calculate (point * P, point * Q, Int N1, Int N2, Long Long Int S, Long Long Int M, Long Long Int & ANS)
{
Int I, J, K, Minx, miny, Maxx, Maxy;
For (I =0 ; I <N1; I ++)
For (J = 0 ; J <N2; j ++)
{
If (MUL (P [I]. x, p [I]. y, Q [J]. x, Q [J]. y) = M & det (P [I]. x, p [I]. y, Q [J]. x, Q [J]. y) = s)
{
Minx = getmin ( 0 , P [I]. X, Q [J]. X );
Maxx = getmax ( 0 , P [I]. X, Q [J]. X );
Miny = getmin ( 0 , P [I]. Y, Q [J]. y );
Maxy = getmax ( 0 , P [I]. Y, Q [J]. y );
If (Maxx-Minx <= N & Maxy-miny <= m)
Ans + = ( Long Long Int ) (N-Maxx + Minx + 1 ) * (M-Maxy + miny + 1 );
}
}
}
Void Deldup ( Int X1,Int Y1, Int X2, Int Y2, Int X3, Int Y3, Long Long Int & ANS)
{
Long Long Int T1, T2, T3;
T1 = sqr (x2-X1) + sqr (Y2-Y1 );
T2 = sqr (X3-X1) + sqr (Y3-Y1 );
T3 = sqr (X3-X2) + sqr (Y3-Y2 );
If (T1 = t2 | t2 = T3 | T3 = T1)
ANS/= 2 ;
}
Void Solve ()
{
Int I, J, K, X1, Y1, X2, Y2, X3, Y3, N1, N2;
Long Long Int S, M, ANS = 0 ;
Scanf ( " % D " , & X1, & Y1, & X2, & Y2, & X3, & Y3 );
Prepare (vec1, x2-X1, Y2-Y1, N1 );
Prepare (vec2, X3-X1, Y3-Y1, N2 );
S = labs (det (x2-X1, Y2-Y1, X3-X1, Y3-Y1 ));
M = MUL (x2-X1, Y2-Y1, X3-X1, Y3-Y1 );
Calculate (vec1, vec2, N1, N2, S, M, ANS );
Calculate (vec2, vec1, N2, N1, S, M, ANS );
Deldup (x1, Y1, X2, Y2, X3, Y3, ANS );
Printf ( " % LLD \ n " , ANS-1 );
}
Int Main ()
{
While (Scanf ( " % D " , & N, & M) = 2 )
{
Solve ();
}
Return 0 ;
}


Problem g:Cross the river 

This question can be done with DP, and there are more than one DP Method.

One of my ideas after I completed this question was to use F [I] [J] to represent the minimum number of stone Piers required for step I to reach position J, in this way, the state transition equation is f [I] [J] = min {f [I-1] [k]} (k <J and K and J cannot exceed K) + (j = Shi Dun), j = Shi Dun means that if J is Shi Dun, the position is 1, and if J is the lotus leaf, the position is 0.

Without optimization, the complexity should be between O (N ^ 2) and O (N ^ 3. I do not really count on the complexity of this algorithm, because if I find that step I can reach the end point, I will directly break, so that if K is small, the number of I cycles will increase, the number of K-cycles decreases, and the number of K-cycles increases, and the number of I-cycles decreases.

The above algorithm can achieve the worst complexity after being optimized with a monotonous queue is O (n ^ 2.

In addition, you can use two arrays to record the minimum number of steps and the minimum number of stone Piers that have passed through, and then use the method similar to the longest ascending subsequence for DP.

 

For other questions, refer to the following issue solving report:

Csgrandeur solution report

Ming solution report

Competition question link (question number is 1170-1178): http://acm.csu.edu.cn/OnlineJudge/problemset.php? Page = 2

Related Article

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.