Sicily 1176 (Dynamic planning)

Source: Internet
Author: User

Topic Connection:Sicily 1176

Problem Solving Ideas:
The title looks like a game of the topic, and like an interval DP (matrix fetch), and it is the difference between the number of the matrix is that he is two people in the number, so each time to an interval, we should be divided into two cases: the first person to take the left and the number of the right, and in the separate consideration of both cases, We are going to get the interval of the previous fetch according to the greedy law. The state equation is a bit complex, directly on the code:

//problem#: 1176//submission#: 3601655//The source code is licensed under Creative Commons attribution-noncommercial-sharealike 3.0 Unported License//uri:http://creativecommons.org/licenses/by-nc-sa/3.0///All Copyright reserved by informatic Lab of Sun Yat-sen University#include <bits/stdc++.h>#define INF 0x3f3f3f3fusing namespace STD;intn,a[1005],dp[1005][1005];intMain () {intt=1; while(~scanf("%d", &n) &&n) { for(intI=1; i<=n;i++)scanf("%d", &a[i]);memset(Dp,-inf,sizeof(DP)); for(intlen=2; len<=n;len+=2)        { for(intI=1; i<=n-len+1; i++) {intj=i+len-1;if(j-i==1) Dp[i][j]=max (a[i],a[i+1])-min (a[i],a[i+1]);Else{inttmpif(a[i+1]&GT;=A[J])//Take the number on the lefttmp=dp[i+2][j]+a[i]-a[i+1];Elsetmp=dp[i+1][j-1]+A[I]-A[J]; dp[i][j]=tmp;if(a[i]>=a[j-1])//Take the number on the righttmp=dp[i+1][j-1]+a[j]-a[i];Elsetmp=dp[i][j-2]+a[j]-a[j-1];                Dp[i][j]=max (DP[I][J],TMP); }                   }        }printf("In game%d, the greedy strategy might lose by as many as%d points.\n", t++,dp[1][n]); }return 0;}

Summary:
In fact, a dynamic programming of the idea to do the game of the topic, you can learn from the matrix to take the number.

Sicily 1176 (Dynamic planning)

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.