poj3186(记忆化搜索)
来源:互联网 发布:淘宝网不能加购物车 编辑:程序博客网 时间:2024/06/04 22:20
FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time.
The treats are interesting for many reasons:
The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.
The treats are interesting for many reasons:
- The treats are numbered 1..N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats.
- Like fine wines and delicious cheeses, the treats improve with age and command greater prices.
- The treats are not uniform: some are better and have higher intrinsic value. Treat i has value v(i) (1 <= v(i) <= 1000).
- Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a.
The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.
Line 1: A single integer, N
Lines 2..N+1: Line i+1 contains the value of treat v(i)
Lines 2..N+1: Line i+1 contains the value of treat v(i)
Line 1: The maximum revenue FJ can achieve by selling the treats
513152
43
Explanation of the sample:
Five treats. On the first day FJ can sell either treat #1 (value 1) or treat #5 (value 2).
FJ sells the treats (values 1, 3, 1, 5, 2) in the following order of indices: 1, 5, 2, 3, 4, making 1x1 + 2x2 + 3x3 + 4x1 + 5x5 = 43.
Five treats. On the first day FJ can sell either treat #1 (value 1) or treat #5 (value 2).
FJ sells the treats (values 1, 3, 1, 5, 2) in the following order of indices: 1, 5, 2, 3, 4, making 1x1 + 2x2 + 3x3 + 4x1 + 5x5 = 43.
题目大意就是给一个序列,每次从序列头或序列尾取出一个数字乘以当前的权重,求能取到的最大值是多少
直接记忆化搜索,n平方的复杂度,太舒服了
AC代码
#include<iostream>#include<cstdio>#include<cmath>#include<queue>#include<algorithm>#include<cstring>using namespace std;int num[2005];int dp[2005][2005]={0};int dfs(int sta,int End,int now){ if(dp[sta][End]&&sta!=End)return dp[sta][End]; if(dp[sta][End])return dp[sta][End]*now;//取出来的不是区间而是单个数字的时候才乘以权重 int ans=max(dfs(sta,End-1,now+1)+dp[End][End]*now,dfs(sta+1,End,now+1)+dp[sta][sta]*now); return dp[sta][End]=ans;}int main(){ int N; while(~scanf("%d",&N)) { for(int i=1;i<=N;i++)scanf("%d",&dp[i][i]); printf("%d\n",dfs(1,N,1)); } return 0;}
阅读全文
0 0
- poj3186(记忆化搜索)
- poj3186(搜索)
- hdu_p1078(记忆化搜索)
- WOJ(记忆化搜索)
- 滑雪 (记忆化搜索)
- hihocoder1037(记忆化搜索)
- hdu1078 (记忆化搜索)
- HDU3555Bomb(记忆化搜索)
- uva10626(记忆化搜索)
- *hdu1078(记忆化搜索)
- 滑雪(记忆化搜索)
- poj1088(记忆化搜索)
- hdu1069(*记忆化搜索)
- uva1637(记忆化搜索)
- hdu5456(记忆化搜索)
- 记忆化搜索(搜索+dp思想)
- poj3186
- POJ3186
- java两种方法代理对象
- java运行环境的安装
- transform的几个知识点
- mt2503 FDN 如何修改成100条
- python入门 准备工作
- poj3186(记忆化搜索)
- PHP操作Redis LIST ,SET, HASH 的相关命令 (一)
- 学生管理系统前奏2
- 关于SpringBoot 集成 Redis
- Oracle listagg用法释义
- 第n次学习后我终于对正则明白了一点点
- USB 虚拟串口简介
- CentOS安装Python2.7与Python2.6并存的方法
- CCF之公共钥匙盒