POJ3186(KB12-O DP)
Treats for the Cows
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 5801 | Accepted: 3003 |
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.
Source
//2017-04-06
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
int v[N], n, dp[N][N];//dp[l][r]表示区间l~r间的最大收益
//状态转移方程:dp[l][r] = max(dp[l+1][r]+day*v[l], dp[l][r-1]+day*v[r]) int dfs(int l, int r, int day)
{
if(l > r)return ;
if(dp[l][r])return dp[l][r];
if(l == r)return dp[l][r] = day*v[l];
return dp[l][r] = max(dfs(l+, r, day+)+day*v[l], dfs(l, r-, day+)+day*v[r]);
} int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i < n; i++)
scanf("%d", &v[i]);
memset(dp, , sizeof(dp));
int ans = dfs(, n-, );
printf("%d\n", ans);
} return ;
}
POJ3186(KB12-O DP)的更多相关文章
- poj3186(区间DP)
题目链接:http://poj.org/problem?id=3186 思路: 区间DP,给treat编号为1..n,状态很明显是上界i和下界j,dp[i][j]表示从下标i到下标j之间数据的最大价值 ...
- POJ3186【区间DP】
题意: 每次只能取两端,然后第 i 次取要val[ i ]*i,求一个最大值 一切都是错觉[读者省略此段] 这道题目一开始想的就是记忆化搜索,然后太天真了?好像是,一开始用一维dp[ i ]直接代表一 ...
- POJ3186:Treats for the Cows(区间DP)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- POJ3186 DP
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5753 Accepted: 29 ...
- kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7949 Accepted: 42 ...
- poj3186(区间dp)
题目链接:http://poj.org/problem?id=3186 题意:给一行n个数,每次可以取出行首或者行末的数,如果第ai是第i次取出的,可以得到ai*i的收益,求最大的总收益: 思路:区间 ...
- POJ3186 Treats for the Cows —— DP
题目链接:http://poj.org/problem?id=3186 Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K To ...
- 「kuangbin带你飞」专题十二 基础DP
layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...
- 各种DP总结
一.数位DP 1.含有或不含某个数“xx”: HDU3555 Bomb HDU2089 不要62 2.满足某些条件,如能整除某个数,或者数位上保持某种特性: HDU3652 B-number Code ...
随机推荐
- PHP中正则表达式函数(Perl兼容)
PHP为使用Perl兼容的正则表达式搜索字符串提供了7个函数,分别是preg_grep().preg_match().preg_match_all().preg_quote().preg_replac ...
- TmsTimeUtils 时间戳
package com.sprucetec.tms.utils; import java.math.BigDecimal;import java.text.DateFormat;import java ...
- Spring Boot中使用@Scheduled创建定时任务
我们在编写Spring Boot应用中经常会遇到这样的场景,比如:我需要定时地发送一些短信.邮件之类的操作,也可能会定时地检查和监控一些标志.参数等. 创建定时任务 在Spring Boot中编写定时 ...
- C#6.0语言规范(十六) 异常
C#中的异常提供了一种结构化,统一且类型安全的方法来处理系统级和应用程序级错误条件.C#中的异常机制与C ++的异常机制非常相似,但有一些重要的区别: 在C#中,所有异常必须由派生自的类类型的实例表示 ...
- 【dpdk】使用libpcap-PMD驱动收发包
ref: Dpdk programmer’s guide 1. 概述 dpdk不仅提供针对物理和虚拟网卡的pmd驱动(Poll Mode Drivers),还提供两个纯软件的pmd驱动,libpca ...
- WebDriver高级应用实例(8)
8.1使用Log4j在测试过程中打印日志 目的:在测试过程中,使用Log4j打印日志,用于监控和后续调试测试脚本 被测网页的网址: http://www.baidu.com 环境准备: (1)访问ht ...
- copy代码的时候,如何去掉代码前边的编号
从网页上拷贝下来的代码前面总有编号,如何去掉! 1.使用正则表达式:在editorplus(notepad++)里按ctrl+h,弹出框里勾选上“正则表达式(regular expression)”, ...
- Linux在终端和控制台下复制粘贴命令快捷键
1.在终端下: (1)复制命令:Ctrl + Shift + C 组合键. (2)粘贴命令:Ctrl + Shift + V 组合键. 2.在控制台下:(即vi编辑过程中) (1)复制命令:Ctrl ...
- Bash算术运算
使用let命令 let let let let let let let 使用expr命令 - ` # + ` # \* ` # / ` # / ` # − \* ` # +` # + -*· # -* ...
- java工具类-日期工具类
1.获得时间戳 为了统一其他语言的如php和unix系统获取的时间戳是10位长度的,精确到秒. java时间戳长度是13位,精确到毫秒 我们获取时间戳需要相应处理. //获取当前时间戳,除以1000, ...