Meow Haha village provocation

Source: Internet
Author: User

The main topic: there are two rows of items, each row has n, Qingjun and dog brother take turns from each row of each side to pick any item. Qingjun initiator, suppose the dog elder brother very smart, blue June of the value of the goods and the maximum of how much?
Analysis: Interval DP.
Status: Dp[x1][y1][x2][y2], which represents the maximum value that can be taken in the interval [x1, y1] and interval [x2, y2].
State transition equation: There are four kinds of cases, namely x1+1,y1-1,x2+1,y2-1. Obviously take four of the smallest one.
In the following code, a B array is used to represent the prefix and the two rows.

#include <cstdio>#include <algorithm>#include <cstring>using namespace STD;intN, a[ -], b[ -];intd[ -][ -][ -][ -], v[ -][ -][ -][ -];intDP (intL1,intR1,intL2,intR2) {if(V[L1][R1][L2][R2])returnD[L1][R1][L2][R2]; V[L1][R1][L2][R2] =1;intsum =0, ans =0;if(L1 <= R1) sum + = A[r1]-a[l1-1];if(L2 <= R2) sum + = B[r2]-b[l2-1];if(L1 <= R1) {ans = max (ans, SUM-DP (l1+1, R1, L2, R2)); ans = max (ans, SUM-DP (L1, r1-1, L2, R2)); }if(L2 <= R2) {ans = max (ans, SUM-DP (L1, R1, l2+1, R2)); ans = max (ans, SUM-DP (L1, R1, L2, r2-1)); }returnD[L1][R1][L2][R2] = ans;}intMain () { while(scanf("%d", &n)! = EOF) {memset(d,0,sizeof(d));memset(V,0,sizeof(v)); for(inti =1; I <= N; i++) {inttmpscanf("%d", &tmp); A[i] = a[i-1] + tmp; } for(inti =1; I <= N; i++) {inttmpscanf("%d", &tmp); B[i] = b[i-1] + tmp; }printf("%d\n", DP (1N1, n)); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Meow Haha village provocation

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.