UVA 10891--game of sum+ classic DP

Source: Internet
Author: User

Topic Link: Click to enter
The last time I did a similar, but only one at a time. This rule can be multiple at a time. Define state Dp[i][j] represents the maximum fraction that can be obtained when facing the interval (i,j), and then Dp[i][j]=max (sum[i+k][j]-dp[i+k][j]+sum I [I-k+1],sum[i][j-k]-dp[i][j-k]+sum[j-k+1][j]). Synthesis is dp[i][j]=sum[i][j]-min (Dp[i+k][j],dp[i][j-k]). where Sum[i][j] Represents the sum of the interval (i,j), 1<=k<=j-i+1. This topic is better with memory word search.

The code is as follows:

#include <iostream>#include <cstdio>#include <cstring>using namespace STD;#define MAXN#define INF 0x3f3f3f3fintA[MAXN],SUM[MAXN][MAXN];intD[MAXN][MAXN];intdpintIintj) {int& Ans=d[i][j];if(ans>-1*inf)returnAnsif(J<i)return 0; ans=-1*inf; for(intk=1; k<=j-i+1; k++) Ans=max (Sum[i][j]-min (DP (I+K,J), DP (I,J-K)), ans);returnAns;}intMain () {intN Freopen ("In.txt","R", stdin); while(scanf("%d", &n) &&n) { for(intI=1; i<=n;i++)scanf("%d", &a[i]); for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) d[i][j]=-1*inf;memset(Sum,0,sizeof(sum)); for(intI=1; i<=n;i++) for(intj=i;j<=n;j++) sum[i][j]=sum[i][j-1]+A[J];intANS=DP (1, n);printf("%d\n", ans*2-sum[1][n]); }return 0;}

UVA 10891--game of sum+ classic 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.