POJ3186:Treats for the Cows(区间DP)
Description
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.
Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally?
The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.
Input
Lines 2..N+1: Line i+1 contains the value of treat v(i)
Output
Sample Input
- 5
- 1
- 3
- 1
- 5
- 2
Sample Output
- 43
Hint
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
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- using namespace std;
- int a[2005],dp[2005][2005];
- int main()
- {
- int n,i,j,k,l,ans;
- while(~scanf("%d",&n))
- {
- for(i = 1; i<=n; i++)
- scanf("%d",&a[i]);
- memset(dp,0,sizeof(dp));
- for(i = 1; i<=n; i++)
- dp[i][i] = a[i]*n;//将对角线初始化
- for(l = 1; l<n; l++)
- {
- for(i = 1; i+l<=n; i++)
- {
- j = i+l;
- dp[i][j] = max(dp[i+1][j]+(n-l)*a[i],dp[i][j-1]+(n-l)*a[j]);//这里是从最后出队的开始往前推,之前的初始化也是为了这里,因为只有最后出队的,i+1才会等于j。
- }
- }
- printf("%d\n",dp[1][n]);
- }
- return 0;
- }
POJ3186:Treats for the Cows(区间DP)的更多相关文章
- POJ3086 Treats for the Cows(区间DP)
题目链接 Treats for the Cows 直接区间DP就好了,用记忆化搜索是很方便的. #include <cstdio> #include <cstring> #i ...
- O - Treats for the Cows 区间DP
FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast am ...
- poj3186 Treats for the Cows(区间)
题目链接:http://poj.org/problem?id=3186 题意:第一个数是N,接下来N个数,每次只能从队列的首或者尾取出元素. ans=每次取出的值*出列的序号.求ans的最大值. 样例 ...
- Treats for the Cows 区间DP POJ 3186
题目来源:http://poj.org/problem?id=3186 (http://www.fjutacm.com/Problem.jsp?pid=1389) /** 题目意思: 约翰经常给产奶量 ...
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- POJ3186 Treats for the Cows —— DP
题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K To ...
- poj3186 Treats for the Cows
http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total S ...
- 【BZOJ】1652: [Usaco2006 Feb]Treats for the Cows(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1652 dp.. 我们按间隔的时间分状态k,分别为1-n天 那么每对间隔为k的i和j.而我们假设i或者 ...
- POJ 3186Treats for the Cows(区间DP)
题目链接:http://poj.org/problem?id=3186 题目大意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最 ...
随机推荐
- Constant is not finite! That's illegal. constant:inf'
原本使用正常的情况, 切换为测试库突然出现这个错误, 网上搜索并排查后得出导致这个问题的原因: (1)就是你的除数为0(2)除数或者被除数为null 找出你出错的界面, 并打断点, 看看是否出现上面两 ...
- 关于Java中的选择排序法和冒泡排序法
一,这种方法是直接传入一个数组进行排序(选择排序法) public static void selectSort(int arr[]){ for (int i = 0; i < arr.leng ...
- Windows Phone 之播放视频
在Windows Phone 7中播放视频有两种方式, (1)使用MediaElement 控件来播放:用MediaElement 控件来播放视频比较灵活,你需要自己去实现播放暂停进度条等等的功能,播 ...
- jquery text--val--html
.html()用为读取和修改元素的HTML标签 对应js中的innerHTML .html()是用来读取元素的HTML内容(包括其Html标签),.html()方法使用在多个元素上时,只读取第一 ...
- django基本命令备忘录
1. 新建一个 django project django-admin.py startproject project-name 新建 app python manage.py startapp ap ...
- ART模式和Dalvik模式的异同
Dalvik模式 如果要解释清楚什么是ART模式,我们就需要从Android系统的应用编译模式说起,我们都知道Android系统是以Linux系统为底层构建的,Android系统是开源(源代码公开)的 ...
- jdk各个版本
http://www.cnblogs.com/langtianya/p/3757993.html
- 对rsync进行封装的shell脚本
抓取 #!/bin/bash . push.sh # 错误处理:尝试查找备份文件 function onError() { local errFile="err" local se ...
- Java实现希尔排序
华杰让我看了一道面试题:现有一段程序S,可以对任意n个数进行排序.如果现在需要对n^2个数进行排序,最少需要调用S多少次?(只允许调用S,不可以做别的操作). 看到了这 ...
- Java 字节码
Java作为业界应用最为广泛的语言之一,深得众多软件厂商和开发者的推崇,更是被包括Oracle在内的众多JCP成员积极地推动发展.但是对于Java语言的深度理解和运用,毕竟是很少会有人涉及的话题.In ...