POJ3186:Treats for the Cows 区间DP
来源:互联网 发布:mac战网外服 编辑:程序博客网 时间:2024/06/05 15:35
题目链接:http://poj.org/problem?id=3186
一:题意:
给定一个序列,序列中有N个元素,我们每次都可以从序列最前面或者最后面取出一个数,
假设按照上面的方法取完所有原序列的数,形成新的序列为a1,a2,,,an,我们为这个
序列定义一个权值:W=a1 * 1 + a2 * 2 + ,,,,,+an*n。题目要求,求出最大的权值。
二,解析:
dp[i][j] 表示区间[ i,j ],能达到的最大权值。
这样可得到递推公式:
dp[x][y]=max(dp[x+1][y]+str[i]*(n-y+x),dp[x+1][y]+str[j]*(n-y+x) );
其中str为原序列,(n-y+x)表示的是取的是第几个数。
三,代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>using namespace std;const int Max=2010;int N;int str[Max];int dp[Max][Max];int DFS(int first,int last){ if(dp[first][last]!=-1) return dp[first][last]; if(first<=last) { int x=DFS(first+1,last)+str[first]*(N-last+first); int y=DFS(first,last-1)+str[last]*(N-last+first); if(x>y) return dp[first][last]=x; return dp[first][last]=y; } return 0;}int main(){ while(scanf("%d",&N)!=EOF) { for(int i=0;i<N;i++) scanf("%d",&str[i]); memset(dp,-1,sizeof(dp)); cout<<DFS(0,N-1)<<endl; } return 0;}
0 0
- POJ3186:Treats for the Cows(区间DP)
- POJ3186:Treats for the Cows 区间DP
- POJ3186:Treats for the Cows(区间DP)
- POJ3186:Treats for the Cows(区间DP)
- POJ3186 Treats for the Cows DP
- POJ3186:Treats for the Cows
- poj3186 Treats for the Cows
- POJ3186 Treats for the Cows
- POJ3186——Treats for the Cows
- 【区间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-3816 Treats for the Cows 【区间DP】
- poj 3186 Treats for the Cows(区间dp)
- poj 3186 Treats for the Cows(区间dp)
- poj 3168 Treats for the Cows 区间dp
- POJ 3186 Treats for the Cows (区间DP 水题)
- ListView Item获取View
- Ibatis相关基础之一-为什么要用sqlMapClientTemplate
- Leetcode#24||Swap Nodes in Pairs
- 将十进制数转换为二进制数
- 有关win32信号量和事件在多线程使用的小例子
- POJ3186:Treats for the Cows 区间DP
- 如何使用Cocos2d-x 3.0制作基于tilemap的游戏:第一部分
- 《集体智慧编程》第5章一些问题
- KMP算法复习
- leetcode 106: Construct Binary Tree from Inorder and Postorder Traversal
- VB.NET 输入IP地址的自定义用户控件(上)
- iOS属性字符串NSMutableAttributedString
- JMeter教程
- 【UI视觉】Android material Ripple 水波 点击效果