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

简单DP,1Y

Posted by bergkamp at 2010-03-03 01:56:52 on Problem 2181
心血来潮,写个记忆化搜索
#include <cstdio>
using namespace std;
int s[150005], dpadd[150005], dpsub[150005];
int dfssub(int);
int dfsadd ( int n )
{
    int i, j;
    if ( dpadd[n] != -1000000 ) return dpadd[n];
    else
    {
        i = dfssub(n-1)-s[n];
        j = dfsadd(n-1);
        dpadd[n] = i > j ? i : j;
        return dpadd[n];
    }
}
int dfssub( int n )
{
    int i, j;
    if ( dpsub[n] != -1000000 ) return dpsub[n];
    else
    {
        i = dfsadd(n-1)+s[n];
        j = dfssub(n-1);
        dpsub[n] = i > j ? i : j;
        return dpsub[n];
    }
}
int main()
{
    int p, i, ans;
    scanf( "%d", &p );
    for ( i = 0; i < p; i++ )
    {
        scanf( "%d", &s[i] );
    }
    for ( i = 0; i < 150005; i++ )
    {
        dpadd[i] = -1000000;
        dpsub[i] = -1000000;
    }
    dpsub[0] = s[0];
    dpadd[0] = 0;
    ans = dfsadd(p-1) > dfssub(p-1) ? dfsadd(p-1) : dfssub(p-1);
    printf( "%d\n", ans );
    return 1;
}

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