Beans (DP, two-time DP)

Source: Internet
Author: User

Beans

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 4141 Accepted Submission (s): 1964

Problem Descriptionbean-eating is a interesting game, everyone owns an m*n matrix, which are filled with different qualiti Es beans. Meantime, there is the only one bean in any 1*1 grid. Now your want to eat the beans and collect the qualities, but everyone must obey by the following rules:if you eat the BEA n at the coordinate (x, y), you can ' t eat the beans anyway at the coordinates listed (if exiting): (x, Y-1), (x, y+1), and The both rows whose Abscissas are x-1 and x+1. Now, how much qualities can do you eat and then get?

Inputthere is a few cases. In each case, there is the integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of beans isn ' t beyond, and 1<=m*n<=200000.

Outputfor the just output the MAX qualities you can eat and then get.

Sample INPUT4 611 0 7 5 13 978 4 81 6 22 41 40 9 34 16 1011 22 0 33 39 6

Sample Output242

Solving: one-to-row DP, one-to-column DP;

Java timed out ... C would not

Code:

import Java.util.Scanner; Public classBeans {Static intA[] =New int[200010], dp[] =New int[200010], dp[] =New int[200010], b[] =New int[200010];  Public Static voidMain (string[] Argvs) {intM, N; Scanner Cin=NewScanner (System.inch);  while(Cin.hasnext ()) {M=Cin.nextint (); N=Cin.nextint ();  for(inti =0; i < M; i++) {Dp[i]=0;  for(intj =0; J < N; J + +) {Dp[j]=0; A[J]=Cin.nextint (); if(J <2) Dp[j]=A[j]; ElseDp[j]= Math.max (Dp[j-2] + a[j], dp[j-1]); } Dp[i]= Dp[n-1]; if(I <2) B[i]=Dp[i]; ElseB[i]= Math.max (B[i-2] + dp[i], b[i-1]); } System. out. println (B[m-1]); }            }}

Beans (DP, two-time DP)

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.