#1037: Digital Triangles

Source: Internet
Author: User

#1037: Digital Triangle time limit:10000msSingle Point time limit:1000msMemory Limit:256MB
Problem description

Little Hi and Little Ho were rewarded with a chance to travel abroad after experiencing Mr. Crab's task, so they came to America on the other side of the ocean. The life of the American people is very interesting, often there are all kinds of strange and strange activities held, this no, small hi and small ho just off the plane, caught up with the local maze festival activities. Maze Festival in the exhibition of the maze are particularly interesting, but small ho is actually not how much like a maze of maze-because this maze of reward is very rich ~

So small ho found a small hi, let small hi help him to get as many prizes, small hi hand one stretch way: "The introduction of the Maze to get!" ”

The maze chosen by Little Ho is a labyrinth of N (n not more than 200) known as the "number Triangle", and the first layer of the maze has I rooms numbered 1. I In addition to the last room, each room will have some stairs to the next floor of the room, with the symbol, that is, from the first floor of the room numbered J, there will be two roads, a room numbered J to the I+1 layer, the other will lead to the i+1 layer numbered j+1 room, And all the rooms on the last floor have only one way out of the maze. Such roads are one-way, which means that little ho will not be able to return to the room again when they go down the road to the next room or leave the maze. There will be only one participant in the maze, and a certain number of lotteries will be generated in each room when each participant enters the maze, which can be redeemed for various prizes after passing through the maze. Small Ho's starting point on the 1th floor of the room numbered 1, and now small ho quietly to other participants to find out the number of lottery tickets in each room, hoping that little hi to help him calculate the maximum number of tickets he can get.

Tip One: Blind greed is not advisable, search calculation is too time-consuming

Tip One: Blind greed is not advisable, search calculation is too time-consuming

Small hi to get the introduction of the maze, think carefully, asked: "Do you have any ideas?" ”

Little Ho thought, the way: "Anyway, there are only two choices at a time, I will choose the way to the room with more lottery tickets bai ~"

Small hi smiled: "You call this blind greed, if I was the creator of the maze, must be in the 2nd floor of the 1th room to put 10 tickets, 2nd Room A not put, and then in the 3rd level of the 3rd room to put 999 tickets, so you have a little petty lost the big head, you say is not?" ”

Little ho startled: "It seems to be true!" Remember not to pound foolish, that ... Since we are all students of computer science, why don't we use a computer to enumerate all the possible paths and find the best one? ”

Little hi nodded: "It is natural to use a computer to solve the problem, but you have to calculate how many of the total number of possible paths, estimate the running time, so as to determine whether it can be completed before the end of the Maze Festival is not ~"

"I come to calculate, starting from the starting point, every time I have two options, and I have to do n-1 times choice, that is, 2 of the n-1, and n maximum may be 200, that is, there is 2^199 path ... Even if I only use one operation per path to figure out the total number of lottery tickets, I'm sure I won't be able to figure it out before I graduate. Qaq "Little Ho came to a very sad conclusion.

"Yes, but this kind of search method actually can have the method to optimize, oh ~" Little hi follow-up guidance.

"What kind of method?" ”

"Don't worry, listen to me slowly."

Hint Two: Memory deep search for divinity, Width priority solution puzzle

Hint Two: Memory deep search for divinity, Width priority solution puzzle

"We can divide the situation, we want to search all the paths, then calculate the profit of each path in turn, is this the problem model we have now?" "Little Hi asked."

Small ho nods: "Carefully analyze this maze, we have two methods of search, one is depth first search, like this picture, we first try to follow the first road of each room to go down, go to the last layer, and then return to the penultimate level, choose the second road to go to the last room, Then return to the third-lowest level, select the second road to the second-to-last level, then select the first road to go to the final level ... During the maintenance of the ticket sum of the room that has been passed, and then each time it reaches the last room, the sum is compared with the global optimal solution ans, and if Sum>ans, replace ans with sum. So after all the paths have been calculated once, ans is stored in the answer we want ~ ~ and the second kind ... "

"First of all, let's see what we can do to optimize this approach." Little hi interrupted the chatter of little ho.

"Oh, but how to optimize it?" Little Ho asked.

"The usual, let's see what is the process of redundancy calculation ~" Little hi or that set of rhetoric, not worried about the little ho heard so many times listen to hate: "Look at this picture, when you enumerate to the green '-" right-"left ' such a path, is not found it and red '-" left-" Right ' This path has reached the second room on the third floor? ”

Yes ”

"When you enumerate to the green path, is the red path already enumerated?" "Little Hi then asked."

Yes “

"Then you see, the green path of the lottery and is 8, the red path of the lottery and is 10, and you are all in the same knot--the 2nd room on the 3rd floor, this time no matter what path the green path continues to follow, I will go from the 3rd floor 2nd room to the beginning of the section after the red path, Will the lottery be more than the green path? "Little Hi," continued the question.

"Well ... Right ~ "

"And the best values in these paths are certainly not more than ans, or ans will be the same at the time, this does not mean that the green path no matter how the next move, there is no way to change the ans, then we can not do the next calculation it?" "Little Hi has made the final conclusion."

"Yes ... For this optimization, we need to record a value of best (I, j)-the maximum number of tickets that can be obtained when reaching the J-room of Level i in the currently searched path, and then check the current sum and the size relationship of good (I, j) each time you enter a room, If sum is less than or equal to best (I, J), there is no need to continue searching. For example, in the previous example, when the 3rd level 2nd room is reached by the green path, best (i, j) = 10, and sum = 8, so there is no need to continue searching down. The clever little Ho quickly summed up the algorithm.

"And this approach, because it is through the previous memory to avoid unnecessary search, so called ' memory search '," Little Ho laughed: "So, as long as it is not the very bad card of your situation--such as the number of tickets in the first J room, J, can be in O (n^2) The complexity of the pass. ”

"And what if I first right and then left?" "Little Ho won't forget."

"That's the number of tickets I have on the first-level J-room I'm I-j" Little Ho's Wicked smile: "You don't have to worry about this, you just have to randomly make sure that almost everything is O (n^2) time complexity, or the average complexity is O (n^2), but your worst-case complexity is definitely not going down, So just think about other ways, like the second way you wanted to say it before? ”

"The nature that corresponds to the depth-first search is the width-first traversal ~ If we use <i, J, k> to arrive at the first J Room of Line I, the current lottery number is k such a state, rewards (I, j) represents the number of lottery tickets in the first floor J room, Then the traversal method is to use a queue, the first, 1, rewards (1, 1) > This state into the queue. And then each time the team first <i, J, K> if i==n, use K to update ans, otherwise it will represent the room can reach the status of two rooms <i + 1, j, K + Rewards (i + 1, j) > and <i + 1, j + 1, K + Rewards (i + 1, j + 1) > put it in the tail until the entire queue is emptied, and ans is the answer. Little Ho slowly came to the road.

"What do you think this place can be optimized for?" ”

"I didn't think of it, but after you said the depth-first search problem I had a little idea, you see the state (2, 1, 8) and the state (2, 2, 6) at the time of processing one will expand out (3, 2, 10) This state, and the other will expand (3, 2, 8) This state, But just like our previous analysis (3, 2, 8) This state is useless, and after that it is no matter how it goes, instead (3, 2, 10) as it does, the lottery is sure to get more. "Little ho comprehend by analogy, I found the problem:" So I can use a best (I, j) as a depth-first search to record the maximum number of tickets that I can get to the J-room in the currently searched path, and then Judge K and best when a state (I, J, K) wants to queue up ( I, j) size, if k is less than or equal to best (i, J) there is no need to enter the team ~ "

"It looks like you're stuck in a dead end, and you're not changing at the worst-case level, are you?" "Little Hi Helpless Way:" You think carefully, according to the order of the width of the first search, is not a state (I, J, K) in the expansion of (I ', J ', K '), (i ', J ', K ') is never in the queue, or still in the queue? ”

"Hmm ... The question seems to be that it is basically such a layer of order out of the queue, that is still the problem, if I judge the state (I, J, K) should be in the queue, found that K is greater than best (I, J), this time if I go to determine whether there is a queue (I, J, K ') status. If not, then (I, J, K) into the queue, or because K ' is greater than k, then as we analyzed before (I, J, K ') This state is useless, instead of using K to replace the state (I, J, K ') in the K ', which will also reduce the operation is not? ”

"Yes, and if you carefully analyze it, each room will only go into the queue and out of the queue once, so that the average time complexity and the worst-case time complexity are O (n^2) Oh!" ~

"This is the case, so now the problem is almost solved!" Little Ho said, "Hurry up and I'll get the prize." ”

"What a hurry!" What knowledge have you learned from this? Little hi stopped Rush's little Ho and asked.

"Ah?" ”

Tip three: summed up the formula to reduce redundancy is the truth

Tip three: summed up the formula to reduce redundancy is the truth

"And do not say depth first such a worst case still problematic method, we say this width first search, we can use Best (i, j) to optimize, simply because this problem exists in such two properties." "Little Hi Way."

"The first is that no matter how I get to the I-level J room, the choice I made before will not make any restrictions or preferential treatment for my choice." Just like if I had to rule at least 3 times to the right, then the state would be more than just (I, J, K), plus a number t that has gone right, so do you think it could be calculated directly like our previous method? If the road to the second floor of the J Room on the way to the right 3 times, then there is no restrictions on the way, otherwise there will be a minimum of a certain number of restrictions on the right to go. So for the two states (I, J, K, L) and (I, J, K ', l '), we can not directly determine that the state is better.

"Yes ~ actually is best (i, J) There are two keywords for reference, and so it is not any two states can be compared." Little Ho concludes.

"The nature of this is what we call a no-no effect, as the name implies, the current choice does not affect the choices that follow." ”

"What about the second nature?" ”

"The second property is what we call the repeating sub-problem, in our previous example, we will start from the beginning to the best way out of the maze into two sub-problems, one is from the 2nd floor of the 1th room out of the maze of the best way, the other is from the 2nd floor of the 2nd room out of the maze of the best way, As long as we can figure out the optimal value of these two parts, I can know the best way to go from the starting point to the maze. "Little Hi Way:" And according to this method, the two sub-problems have a same sub-problem-from the 3rd floor of the 2nd room to go out of the maze of the best way. ”

"This is the repetition of the sub-problem, and our approach is that the repeated sub-problem is only counted once!" That means we have to figure out the best route to the 2nd room from the starting point to the 3rd floor, and then think about how to get there-"Little Ho feels like he's getting the key."

"Yes, so combined with such two properties, we may consider using best (I, j) to indicate the maximum number of lotteries that can be obtained from the starting point to the first-level J room. "Little Hi has proposed a new definition:" So the answer we ask is the biggest one of all Best (N, j), right?

"Yes, then how to beg for this ..." Little ho pondered: "The first-floor J-room can be reached by the first room of section i-1 and the J-1 Rooms of section i-1, so Best (i, j) = Max{best (I-1, J-1), Best (I-1, J)} + Rewar DS (I, j) so? ”

"Yes, this formula is what we commonly called the state transfer equation, and what you have to do is to calculate the best (I, j) for each room in this order, and then, at the end of the last statistic, the maximum (n, j), you can get the answer." ”

"This process and the width-first search are really similar, but because the order is determined, I can use a double loop, the outer layer is I from 1 to n the enumeration layer, the inner level is J from 1 to I enumeration room number, you can directly calculate!" "Little Ho Road."

"You have to consider the boundary of this formula, such as j=1 when you do not need to take max, directly using Best (I-1, j) calculation can ~" Small hi pointed out the existence of small ho problem.

"Oh ~ so the j=i also have similar treatment, right?" ”

"Yes!" So go to write the program ~ "Small Hi smiled:" Seize the time, the Maze Festival is about to end Oh! ”

Input

Each test point (input file) has and has only one set of test data.

The first behavior of each set of test data is a positive integer n, which represents the number of layers in the maze.

The next n lines describe the number of lotteries in each room in the maze, where the number of J on line I represents the amount of lottery tickets in the room numbered J of the maze layer I.

Test data assurance that 100% of the data satisfies n not more than 100

For 100% of the data, the number of layers N of the maze is not more than 100

For 100% of the data, the number of lotteries in each room does not exceed 1000

For 50% of the data, the number of layers in the maze is not more than 15 (small ho means that 2^15 is more than 30,000, which means ...). )

For 10% of the data, the number of layers in the maze is not more than 1 (little hi is curious about how your boundary situation is handled?). ~

For 10% of the data, the maze is constructed to satisfy: For more than 90% of the nodes, the left road leads to the number of lottery tickets in the room to the right road leading to the number of lottery tickets.

For 10% of the data, the maze is constructed to satisfy: For more than 90% of the nodes, the left road leads to a room with fewer lotteries than the right road leads to the room.

Output

For each set of test data, output an integer ans, which indicates the maximum number of lotteries that small ho can obtain.

Sample input
526 41 2 84 0 9 66 5 5 3 6
Sample output
28
AC Code: 
#include <bits/stdc++.h>using namespace Std;int dp[1010][1010];int main () {    int t,maxx=0;    scanf ("%d", &t);    for (int i=1; i<=t; i++)    {        for (int j=1; j<=i; j + +)        {            scanf ("%d", &dp[i][j]);            Dp[i][j]+=max (Dp[i-1][j],dp[i-1][j-1]);            Maxx=max (Maxx,dp[i][j]);        }    }    printf ("%d\n", Maxx);}


#1037: Digital Triangles

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.