Day4: T1 tips (similar to pointer operations) T2 search + small details

Source: Internet
Author: User
Day4: many tips: gett1

I have never heard of a slightly specialized term like this, and it is a bit similar to pointer operations. I have encountered many questions like this before.

Every time it appears in a different form, but I feel like my thoughts are a little close (for example, if one day I have a question of "happy", it seems to be the same type)

 

At first glance, this type of questions always seem very difficult to write. In fact, after thinking of these skills, it is very simple.

It seems that there is no regularity or template.

Probably, it's pointer thinking + usual accumulation.

 

Let's talk about this question.

Before analyzing the correct solution, let's talk about the easy-to-think fraud method.

Set the number of men and women to the same number of ANS = 0, if the next is a man-> ans ++, else ans --;

When ans = 0 is found, record the position at this time and subtract this position from the previous one.

Scan the answer again and add the answer continuously.

This method is simple and easy to think of, but it is only an O (n2) algorithm that can only pass 50% of the data.

 

What is the positive solution?

In fact, this is the improvement of the above fraud methods;

Think about the fact that we don't need to look for ans = 0. We only need to see the same ans value in the front and back, that is, we can know that the numbers of men and women are the same (in other words, how many or less boys are the same as girls)

Use an array POS [I] to record the position where "1" appears for the first time when there are more than "0" (I can be a negative number, indicates that the number of "1" is smaller than "0 ).

When scanning at a certain position W, we find that POS [x] has a value, and we get the sub-sequence from POS [x] + 1 to W number to meet the conditions, therefore, W-pos [x] is used to update the maximum length of sub-sequences that meet the conditions. To facilitate processing when X is 0, we stipulate that POS [0] = 0.

for i:=1 to n do    begin      read(x);      if x=1 then inc(duo) else dec(duo);      if (a[duo]=0) and (duo<>0) then a[duo]:=i        else if i-a[duo]>ans then ans:=i-a[duo];    end;

A great idea, mainly to understand and use array A, when a [duo]! When the value is 0, the location is recorded.

However, this method also cleverly avoids the need to find continuous intervals in the lie-in method, because it is the first place to record duo's first appearance !!

Mark

// By the way, the array in C ++ does not have a negative number, so it is best to assign duo 100000

T2: Search

Is a great question

Obviously, this is a typical search question, but how to search and think about it is actually the key to the problem.

First of all, thinking cannot only limit how people go. After leaving, how can we determine whether it can be shot?

If you think so, the written program will be very complicated.

Is there a simpler idea?

The answer is yes.

Another way of thinking is to imagine that a person is not moving, and the human can be shot at a time;

Then move the target point and search for the target point and move it to the point where the target person can be shot. This will not solve the problem!

Qaq... everything is an IQ error !!

 

After thinking clearly, you only need to carefully pre-process the eight points that can be shot in the azimuth.

In BFs, you can:

Code attached:

Const DX: array [1 .. 4] of-1 .. 1 = (,-); // the start point is extended in four directions. DY: array [1 .. 4] of-1 .. 1 = (-,); var n, m, I, j, X1, Y1, X2, Y2, CX, Cy: longint; Map: array [-10 .. 200,-10 .. 200] of char; HX, Hy, HD: array [1 .. 20000] of longint; // queue Bo: array [-10 .. 200,-10 .. 200] of Boolean; function BFS (X, Y: longint): Boolean; var Chong: array [-10 .. 200,-10 .. 200] of Boolean; // judge head, tail, I, j, X3, Y3, X4, Y4, long: longint; begin fillchar (HX, sizeof (HX ), 0); fillchar (Hy, sizeof (Hy), 0); fillchar (HD, sizeof (HD), 0 ); BFS: = false; fillchar (Chong, sizeof (Chong), false); Head: = 0; tail: = 1; HX [1]: = X; hy [1]: = y; HD [1]: = 0; Chong [x, y]: = true; while head <tail do // extend begin Inc (head) from the starting point to all directions; X3: = HX [head]; Y3: = hy [head]; long: = HD [head]; for I: = 1 to 4 Do begin X4: = X3 + dx [I]; Y4: = Y3 + dy [I]; if (X4> = 1) and (X4 <= N) and (Y4> = 1) and (Y4 <= m) and (Map [X4, y4] = 'O') and (not Chong [X4, Y4]) Then // you need to judge begin Chong [X4, Y4]: = true; if Bo [X4, y4] Then B Egin writeln (long + 1); exit (true); end; Inc (tail); HX [tail]: = X4; hy [tail]: = Y4; HD [tail]: = long + 1; end; begin assign (input, 'Dragon. in '); reset (input); assign (output, 'Dragon. out'); rewrite (output); readln (n, m); for I: = 1 to n do begin for J: = 1 to M do read (Map [I, j]); readln; end; readln (X2, Y2, X1, Y1); While (X1 <> 0) or (Y1 <> 0) or (X2 <> 0) or (Y2 <> 0) Do // preprocessing the following parts in all directions can be scaled into a process begin fillchar (Bo, Sizeof (BO), false); Bo [X2, y2]: = true; for I: = Y2 to M do if map [X2, i] = 'O' then Bo [X2, I]: = true else break; for I: = y2-1 downto 1 do if map [X2, i] = 'O' then Bo [X2, I]: = true else break; // horizontal for I: = x2 to n do if map [I, y2] = 'O' then Bo [I, y2]: = true else break; for I: = x2-1 downto 1 do if map [I, y2] = 'O' then Bo [I, y2]: = true else break; // portrait CX: = x2; Cy: = Y2; while (CX> = 1) and (CY <= m) and (Map [CX, Cy] = 'O') Do // upper right begin Bo [CX, Cy]: = true; Dec (CX); Inc (CY); end; CX: = x2; Cy: = Y2; while (CX <= N) and (CY> = 1) and (Map [CX, Cy] = 'O') Do // in Bo [CX, Cy]: = true; Inc (CX ); dec (CY); end; CX: = x2; Cy: = Y2; while (CX> = 1) and (CY> = 1) and (Map [CX, cy] = 'O') Do // begin Bo [CX, Cy]: = true; Dec (CX); Dec (CY); end; CX: = x2; cy: = Y2; while (CX <= N) and (CY <= m) and (Map [CX, Cy] = 'O ') do // begin Bo [CX, Cy]: = true; Inc (CX); Inc (CY); end; // the above four paragraphs are diagonal lines I F Bo [X1, Y1] Then writeln (0) else // if it can be achieved directly, 0 if not BFS (x1, Y1) Then writeln ('impossible! '); Readln (X2, Y2, X1, Y1); end; close (input); close (output); end.

 

Day4: T1 tips (similar to pointer operations) T2 search + small details

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.