The fourth training camp-March 13, 2011

 The topic this time is disgusting.

       The first four questions are USACO questions. The first question is the prime number square matrix that has been done before, the second question is stamps, the third question is ugly numbers, and the fourth question is milking. 

       The first question is a very skillful search question.

       At first I thought of enumerating the last row and the last column, then enumerating the first column, the main diagonal and the first row, then determining the sub-diagonal, and finally calculating the remaining four, as shown in the figure

3 5 5 5 2
3 4 7 6 2
3 7 4 7 2
3 6 7 4 2
1 1 1 1 0

Finally, judge whether the middle three rows and three columns are satisfied, but this is a serious timeout! ! !

The main reasons are as follows:

1) Use the <vector> container to store several preprocessed arrays

type1[I]: All digits ending with I are prime numbers with odd numbers;

type2[I]: prime numbers that do not contain 0

type2[I]: Prime number

type3[I][J][K]: Prime numbers starting with I, J in the middle, and K at the end

(All of which are 5 prime numbers)

Reason analysis: the container is slow to read;

Solution: use array access, not too big;

 

2) Use get(n,k) to get the k-th position of the number n

Reason analysis: Since this is at the bottom of the enumeration, it needs to be greatly improved;

Solution: preprocess a 5-dimensional array, and separate the 5 digits in the process of finding prime numbers, and only O(1) judgments for the newly generated ones;

 

3) All are processed at the last step

Reason analysis: some can be cut directly, for example, the 7 above and the 7 below can be determined without 5;

Solution: Change the enumeration order, and determine what can be determined first,

The enumeration order is changed to 1->2->4->3->6->5

The newly formed second and fourth lines can be judged without allowing 5;

 

Time from 10s+ ----> 5s+ ----> 2s+ ------> 1s-

The last thing measured on USACO is

After reading the answers on the Internet, there is a better enumeration order (bold ones can be deduced)

   0 20 22 23   8  
  13 1 17   7 15
  9 10 2 11 12
  14   6 18   3 16
   5 19 21 24   4
The slowest time is 0.2s

 

Enlightenment:

1. Don't use <vector> casually, use it when the length of the array is too large, and determine the time spent;

2. The order of enumeration is very important, first enumerate the most important points;

3. Always pruning;

 

The code is too long, there is no need to post it~~~

 

Question 2: The question is very good, very confusing, it is actually a BFS question!

The third question: The question is very good, you must save the pointer corresponding to each prime number!

Fourth question: compare water~~

 

Question 5:

The main idea of ​​the topic: What is the minimum number of steps to go from (x1, y1) to (x2, y2)? (x,y >= 0, <= 1000000)

Algorithm: Greedy + BFS

Analysis: The data is so big! ! ! Can only be greedy! ! !

First walk in the (3,3) way, then walk in the (0,4) or (4,0) way, until it is within 200 (a certain range).

 

Sixth question: dynamic programming

Pay attention to the initial value

ps: Data kills people! ! The title description kills people!

 

Question 7:

Title description: Given N (100000) piles of stones, then merge the two piles to form a new pile each time. The physical strength is the sum of the two piles. Find the minimum physical strength.

Analysis: The data is so large, there are two ways.

1) Use heap to do;

2) Do it with a monotonous queue !

 

Question 8:

Title description: Find the 0 matrix with the largest area in the 01 matrix.

Algorithm: dynamic programming

analysis:

1) First preprocess the number of zeros that are continuous on the left and continuous on the right;

2) Then enumerate each state of 1 (up) and 0 (down) at a time to find the maximum number that can be searched down, and save the least number on the left and right sides

3) But the above algorithm approximates a cube, can it become a square?

     Assuming that there is an optimal solution, there must be a 1 at the top (otherwise you can continue to expand the size), so as long as the above process is first enumerated, in the enumerated row, the process can be optimized to the square!

 

Guess you like

Origin blog.csdn.net/zjsxzjb/article/details/6246691