Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:太可耻了,居然企图用递归过……

Posted by adamgic at 2005-12-23 05:11:47 on Problem 2738
In Reply To:太可耻了,居然企图用递归过…… Posted by:adamgic at 2005-12-23 05:11:07
#include<iostream>
using namespace std;
int i,n,card[1000],gameid=1,win,sum;
int DP(int start,int end)
{
    int a,b,c,d;
    if(start+1==end)return max(card[start],card[end]);
    if(card[start+1]>=card[end]){a=start+2;b=end;}
    else {a=start+1;b=end-1;}
    if(card[start]>=card[end-1]){c=start+1,d=end-1;}
    else {c=start;d=end-2;}
    return max(card[start]+DP(a,b),card[end]+DP(c,d));  
}
int main()
{
    while(cin>>n && n){
        sum=0;
        for(i=0;i<n;i++){
            scanf("%d",&card[i]);
            sum+=card[i];
        }
        win=DP(0,n-1);
        printf("In game %d, the greedy strategy might lose by "
                "as many as %d points.\n",gameid++,2*win-sum);
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator