POJ 3186 Treats for the Cows(区间DP)

来源:互联网 发布:本地系统 网络受限 编辑:程序博客网 时间:2024/06/04 01:29

the question is here
this DP question want us to chose treats and find the maximum value . We can see this is a dp question , and the equation which I write below means the maximum value for i to j can be decided by the sub region .

#include <iostream>#include <cstring>#define ll long long#define maxn 105using namespace std;//dp[i][j] = max{dp[i+1][j]+a[i]*(n+i-j),dp[i][j-1]+a[j]*(n+i-j)}int dp[2005][2005];int v[2005];int main(){    int n=0;    cin>>n;    for(int i=1;i<=n;i++)    {        cin>>v[i];    }    for(int i=1;i<=n;i++)    {        dp[i][i] = v[i]*n;  // init     }    for(int i = n;i>0;i--)    {        for(int j=i;j<=n;j++)        {            dp[i][j]  = max(dp[i+1][j]+v[i]*(n+i-j),dp[i][j-1]+v[j]*(n+i-j));        }    }    cout<<dp[1][n]<<endl;    return 0;}