POJ 3186Treats for the Cows 简单dp
来源:互联网 发布:54美工网 编辑:程序博客网 时间:2024/05/17 03:18
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=68966#problem/O
题意:给定一个数组,每次只能从首或位取一个数,每个数的权值为取该数的序数,求所能使所有数乘上他的权值的和值最大,并求出该最大和值。
虽然说是简单dp,但是,还是百度了一下,说是枚举数组的位置,然后就很容易想到状态转移方程了。
思路:用dp[i][j]表示:剩下的数组的状态,i表示余下数组的首位置,j表示余下数组的末位置。则状态转移表示为:
dp[i][j]=max(dp[i-1][j]+A[i-1]*t,dp[i][j+1]+A[j+1]*t);其中t表示取i-1或j+1时的序数。最后只能得到dp[i][i],最后还需计算一下最大值。
代码:
#include<iostream>#include<cstdio>#define maxn 2010using namespace std;int N,A[maxn],dp[maxn][maxn];int main(){ //freopen("in.txt","r",stdin); while(cin>>N){ for(int i=1;i<=N;i++) scanf("%d",A+i); dp[1][N]=0; for(int i=2;i<=N;i++) dp[i][N]=dp[i-1][N]+A[i-1]*(i-1);//初始化 for(int j=N-1;j>=1;j--) dp[1][j]=dp[1][j+1]+A[j+1]*(N-j);//初始化 for(int i=2;i<=N;i++) for(int j=N-1;j>=1;j--){//枚举 int t=(N-j+i-1); dp[i][j]=max(dp[i-1][j]+A[i-1]*t,dp[i][j+1]+A[j+1]*t); } int ans=0; for(int i=1;i<=N;i++)//最后一个数得取掉,并更新答案 ans=max(ans,dp[i][i]+A[i]*N); cout<<ans<<endl; } return 0;}
0 0
- POJ 3186Treats for the Cows 简单dp
- POJ 3186 Treats for the Cows (DP)
- 【区间dp】Treats for the Cows POJ
- poj 3186 Treats for the Cows(DP)
- poj 3186 Treats for the Cows (区间dp)
- 【POJ 3186 】Treats for the Cows (区间DP)
- POJ 3186 Treats for the Cows(区间DP)
- poj 3186 Treats for the Cows(区间dp)
- poj 3186 Treats for the Cows(区间dp)
- POJ 3186 Treats for the Cows (区间DP 水题)
- POJ - 3186 Treats for the Cows(DP)
- POJ 3186 Treats for the Cows(区间DP)
- POJ 3186 Treats for the Cows (区间DP)
- 区间DP-POJ-3186-Treats for the Cows
- poj 3186 Treats for the Cows(区间dp)
- POJ 3186 Treats for the Cows (区间DP)
- poj 3186 Treats for the Cows (区间DP)
- poj 3186 Treats for the Cows(区间dp)
- 数组a[]存放1至N-1个数,其中某个数重复一次,写个函数找出那个数:
- Arcgis for android 离线查询
- activeMQ 备忘
- android SoundPool类测试
- NHibernate 执行存储过程
- POJ 3186Treats for the Cows 简单dp
- 排序算法实现与比较
- 获取toast的Text的值
- APP的Android.mk文件分析
- UTF的字节序和BOM
- jQuery遍历对象/数组/集合
- Oracle存储过程和程序包
- AccelerometerPlayActivity
- jquerymobile之collapsible可折叠块题目内容动态显示