ACM-scenic spots (AC, dynamic planning, maximum subsequences and problems)

Source: Internet
Author: User

Scenic Spots

Time Limit: 1000 MS Memory Limit: 65536 K
Total Submit: 182 Accepted: 35

Description

DieIng will go to tourism on May Day. The roads of scenic spots in tourist areas are distributed. The roads of scenic spots are east-west directions. Each road has DieIng's favorite value. The north and south directions are forest trails for rest.

Due to the large number of tourists on May Day, the tourist area requires that the roads to enjoy scenic spots can only walk one way, from East to West, and the forest trails can walk two ways.
DieIng requires you to help him design the route (starting from any point and ending from any point) so that he can play the Most high. (Maximum sum of favorite values)

Input

The first line is two integers, N (0 <= N <= 10) and M (0 <= M <= 1000000), representing the layout of the tourist area, N * M

Next N rows, each row has M integers favorite (-100 <= favorite <= 100)
N, M = 0.

Output

Output DieIng to get the sum of favorite values.

Sample Input

3 45 -4 6 14-2 -48 11 -89 -13 4 80 0

Sample Output

30

Source

GDUT Programming Contest 2009 by longshen

 

 

/* Idea: 1. When you enter the maximum value for a question, the first thought is to use DP or greedy. Then, you can check whether there is any optimal sub-structure, finally, the maximum number of scenic spots passed must be the maximum value through the previous scenic spots. If there are any independent sub-problems, try DP first. 2. When writing recursive solutions, I suddenly thought this question seemed to have been seen. The sample output of the question is 30, that is, the maximum value of each column. If you use the brute-force method to solve the problem, you need to traverse the maximum value of each column and then calculate the maximum value based on the sum of values one by one. In this case, it is estimated that it will time out. Can it reduce the time complexity? I was prompted when I said "In fact, it is to find the maximum value of each column. I can find the maximum values of each column and place them in another array or overwrite the position of the element in the original array to construct an array consisting of the maximum values of the column, then let's take a look. This problem is actually converted into the problem of finding the sum of the largest subarrays. For details, see the code ---------------------------------------------------------------------------- */# include <stdio. h> # define max (x, y) (x)> (y )? (X): (y) long a [1000004]; long B [1000004]; // The space can also be optimized to overwrite the original position int main (void) {long N = 0; long M = 0; long I = 0; long j = 0; long nFavSum = 0; long nTemp = 0; long nTotal = 0; while (scanf ("% ld", & N, & M), (N! = 0 & M! = 0) {nFavSum = 0; nTemp = 0; nTotal = 0; for (I = 0; I <N; ++ I) {for (j = 0; j <M; ++ j) {scanf ("% ld", & nTemp); if (0 = I) {a [nTotal ++] = nTemp ;} else if (nTemp> a [j]) {a [j] = nTemp ;}} B [0] = a [0]; nFavSum = 0; for (I = 1; I <M; ++ I) {B [I] = max (a [I], B [I-1] + a [I]); if (nFavSum <B [I]) {nFavSum = B [I] ;}} printf ("% ld \ n", nFavSum) ;}return 0 ;}

 

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.