POJ 3186 Treats for the Cows

Source: Internet
Author: User

POJ 3186 Treats for the Cows

The first thought was to use a flag to record the time for retrieving the number of I, but later I found that this method is not good, there are too many possibilities, and there is no way to push it.

Then let's take a look at the ideas of the report, saying that the dp is used, and the status is set up. However, due to the influence of the inherent thinking, I always want to push it along.

Finally, I couldn't think of it. After reading the code, I found that I had to push it backwards. I started to push it from the end, so the formula came out.

To ensure that the values used in each layer of the loop have been calculated, enumerate by Interval Length.

To commemorate the first dp Interval

 

/* Dp [I] [j]: When I to j items are left, remove the maximum value that can be obtained for all the remaining items dp [I] [j] = max (dp [I + 1] [j] + v [I] * (n-(j -I + 1) + 1), dp [I] [J-1] + v [j] * (n-(j-I + 1) + 1); */# include

 
  
# Include

  
   
Using namespace std; int n, v [2005], dp [2005] [2005]; void init () {// memset (dp,-1, sizeof (dp )); for (int I = 1; I <= n; I ++) dp [I] [I] = v [I] * n;} int main () {# ifndef ONLINE_JUDGEfreopen ("in.txt", "r", stdin); # endifscanf ("% d", & n); for (int I = 1; I <= n; I ++) scanf ("% d", & v [I]); int tmp; init (); int j; for (int l = 2; l <= n; l ++) {// reverse push, starts from the beginning of the last outbound queue, enumerative by Interval Length for (int I = 1; I <= n; I ++) {j = I + L-1; dp [I] [j] = max (dp [I + 1] [j] + v [I] * (L-1 )), dp [I] [J-1] + v [j] * (L-1);} printf ("% d \ n ", dp [1] [n]);}

  

 


 

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.