"Problem description"
Xiao Ming's birthday, his father gave him a pair of turtle chess as a gift. The chess board is a row of n squares, one score per lattice (non-negative integer). The 1th grid is the only starting point, nth grid is the end, the game requires the player to control a turtle piece from the starting point to go to the end. The M-card crawl card in Tortoise chess, divided into 4 different types (m card does not necessarily contain all 4 types of cards, see sample), each type of card is labeled with 1, 2, 3, 44 digits, indicating that after the use of this card, the turtle pieces will crawl forward the corresponding number of squares. In the game, each time the player needs to select a crawl card from all the crawl cards, and control the corresponding number of the turtle pieces forward, each card can only be used once. In the game, the turtle piece automatically obtains the starting point lattice the fraction, and in the subsequent crawl each arrives a lattice, obtains the corresponding score of the lattice. The player's final game score is the sum of all the squares of the turtle pieces from the beginning to the end of the process. Obviously, the use of different crawl cards in the order will make the final game score different, Xiaoming wants to find a card in order to make the final game score the most. Now, tell the score of each lattice on the board and all the crawling cards, can you tell xiaoming how many points he can get?
"Sample Input"
8
4 96 10 64 55 13 94 53 5 24 89 8 30
1 1 1 1 1 2 4 1
"Sample Output"
455
"Data Range"
There is 1≤n≤30,1≤m≤12 for 30% of the data.
For 50% of the data there are 1≤n≤120,1≤m≤50, and 4 kinds of crawling cards, each card will not exceed 20 number of sheets.
For 100% of the data is 1≤n≤350,1≤m≤120, and 4 kinds of crawling cards, each card will not exceed the number of 40;0≤ai≤100,1≤i≤n
1≤bi≤4,1≤i≤m.
"Problem-solving ideas"
This is titled NOIP2010 raise the second question, at that time the talent primary school, naturally did not participate ... just see this problem, the first thought is greedy, the following found the classification of the problem in the recursion, DP inside, and then forcing themselves to DP think ... Think about it and think about it ...
I think of the traditional method, set f[i,j,k,l] for the use of I walk 1 steps of the card, J Walk 2 steps of the card, K Zhang Walk 3 steps of the card, L Walk 4 steps of the card to get the value, then f[i,j,k,l] only with the value before the card used, obviously has no effect, one by one enumeration of each card , plus the value of this lattice, is the desired value. The end result is when all cards are exhausted, i.e. f[b[1],b[2],b[3],b[4]]. (b array stores the number of each card, see Code)
Listen to the teacher said can be solved with three-dimensional array, to see the god Ben attached ideas in the comments, greatly appreciated!
"Motion regulation Equation"
F[i,j,k,l]:=max (f[i-1,j,k,l],f[i,j-1,k,l],f[i,j,k-1,l],f[i,j,k,l-1]) +a[i+j*2+k*3+l*4+1] Note to add 1, Because from the first lattice to go.
"Boundary Condition"
This problem is not required boundaries, as to why you can think for yourself ...
"Code Implementation"
1 uses math;2 varF:Array[-1.. +,-1.. +,-1.. +,-1.. +] ofLongint;3A:Array[1.. -] ofLongint;4B:Array[1..4] ofLongint;5 I,j,k,l,n,m,x:longint;6 begin7 readln (n,m);8 fori:=1 toN Do9 read (a[i]);Ten fori:=1 toM Do One begin A read (x); -Inc (B[x]);//Store the number of cards per type - End; the fori:=0 tob[1] Do - forj:=0 tob[2] Do - forl:=0 tob[3] Do - fork:=0 tob[4] Do +F[i,j,l,k]:=max (Max (f[i-1, j,l,k],f[i,j-1, L,k]), Max (f[i,j,l-1, k],f[i,j,l,k-1])) +a[i+j*2+l*3+k*4+1];//Dynamic Planning -Writeln (f[b[1],b[2],b[3],b[4]]); + End.
Turtle Chess
Turtle Chess (Wikioi 1068)