Lightoj1031_ interval DP

Source: Internet
Author: User

Title Link: http://lightoj.com/volume_showproblem.php?problem=1031

Title Description:

Give a series, two people take turns to fetch the number, finish. Can take a lot of numbers at a time, but only from the first or the end of the beginning to take a number of consecutive. Q. What is the maximum value of the last two?

Interval DP;

I've been reading a few ladder reports before I figured it out, and now I think it fits the requirements of dynamic planning.

D (I, j) represents the maximum value that a number of people can fetch in the array I to J, and then enumerates the split points in the middle,

ans = max (ans, sum[k]-sum[i-1]-d (k+1, J));
ans = max (ans, sum[j]-sum[k-1]-d (i, k-1));

Ways to adopt and memorize searches

1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdlib>5#include <cstdio>6#include <vector>7#include <ctime>8#include <queue>9#include <list>Ten#include <Set> One#include <map> A using namespacestd; - #defineINF 0x3f3f3f3f -typedefLong LongLL; the  - inta[ the], sum[ the], dp[ the][ the]; - intSolveintLintR) - { +     if(Dp[l][r]! =-1*INF) -         returnDp[l][r]; +     intres = sum[r]-sum[l-1]; A      for(inti = l; I <= R; i++) at     { -res = max (res, sum[i]-sum[l-1]-solve (i+1, R)); -res = max (res, sum[r]-sum[i-1]-solve (L, I-1)); -     } -DP[L][R] =Res; -     returnRes; in } - intMain () to { +     intt, N; -scanf"%d", &t); the      for(intCA =1; CA <= T; ca++) *     { $scanf"%d", &n);Panax Notoginsengsum[0] =0; -          for(inti =1; I <= N; i++) the              for(intj = i; J <= N; J + +) +DP[I][J] =-1*INF; A          for(inti =1; I <= N; i++) the         { +scanf"%d", &a[i]); -Dp[i][i] =A[i]; $Sum[i] = sum[i-1] +A[i]; $         } -printf"Case %d:%d\n", CA, Solve (1, N)); -     } the     return 0; -}
View Code

Lightoj1031_ interval 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.