kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7949 | Accepted: 4217 |
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.
题目大意:给一个长度为n的序列,每次只能从队首或队尾取一个数,第几次取 * f[i] 就是利润,求最大利润。
看到题目果断贪心,只能局部最优,因为是dp专题,但是丝毫不会dp,看了题解发现是区间dp,然后看着理解了一下,
dp[i][j] 表示 从第i个数到第j个数的最大利润,由于只能从dp[i+1][j] 和 dp[i][j-1]到达dp[i][j],所以状态转移方程可以表示为 dp[i][j] = max(dp[i+1][j] + f[i] * (n-j+i), dp[i][j-1] + f[j] * (n-j+i),
这里是由里向外递推的,逆向遍历i。用n-j+i表示第几次取(可以模拟一个简单的看看)
初始化条件要注意一下,dp[i][i] = f[i] * n 只有一个数时,它就是最后取的。
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define mem(a,b) memset((a),(b),sizeof(a))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF =0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
const ll mod = 1e9+;
//head
const int maxn = + ;
int dp[maxn][maxn], f[maxn];//dp[i][j] 表示从i到j的 最大值 int main() {
int n;
while(~scanf("%d", &n)) {
for(int i = ; i <= n; i++)
scanf("%d", &f[i]);
mem(dp, );
for(int i = ; i <= n; i++)//初始化 每一个都是最后取的
dp[i][i] = f[i] * n;
for(int i = n - ; i >= ; i--) {//从里往外推
for(int j = i + ; j <= n; j++) {// n - j + i 很巧妙
dp[i][j] = max(dp[i+][j] + f[i] * (n - j + i), dp[i][j-] + f[j] * (n - j + i));//转移方程
}
}
printf("%d\n", dp[][n]);
}
}
kuangbin专题十二 POJ3186 Treats for the Cows (区间dp)的更多相关文章
- kuangbin专题十二 HDU1074 Doing Homework (状压dp)
Doing Homework Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- POJ3186:Treats for the Cows(区间DP)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- 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 ...
- kuangbin专题十二 POJ1661 Help Jimmy (dp)
Help Jimmy Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14214 Accepted: 4729 Descr ...
- kuangbin专题十二 HDU1176 免费馅饼 (dp)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- kuangbin专题十二 HDU1078 FatMouse and Cheese )(dp + dfs 记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- kuangbin专题十二 HDU1260 Tickets (dp)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
随机推荐
- 使用AJAX异步提交表单的几种方式
方式一 手工收集所有的用户输入,封装为大的“k1=v1&k2=v2…”键值对形式,使用$.post(url, data,fn)把数据提交给服务器 $.ajax({ type:'post', u ...
- 类型:JQuery;问题:ajax调用ashx文件;结果:ashx文件怎么获取$.ajax()方法发送的json数据
ashx文件怎么获取$.ajax()方法发送的json数据 作者:careful 和ajax相关 新浪微博QQ空间QQ微博百度搜藏腾讯朋友QQ收藏百度空间人人网开心网0 $.ajax({ t ...
- kernel下制作动态logo
kernel下制作动态logo 在uboot中实现logo的好处是反映速度快. 在kernel中实现logo的好处是,不管是android还是什么其他平台,logo显示无需考虑上层平台. 参照三星平台 ...
- ActiveMQ (二) JMS入门
JMS入门 前提:安装好了ActiveMQ ActiveMQ安装 Demo结构: 首先pom.xml引入依赖: <dependency> <groupId>org.apach ...
- Codeforces 8C 状压DP
题意:有个人想收拾行李,而n个物品散落在房间的各个角落里(n < 24).现在给你旅行箱的坐标(人初始在旅行箱处),以及n个物品的坐标,你一次只能拿最多两个物品,并且拿了物品就必须放回旅行箱,不 ...
- bzoj1951 组合数取模 中国剩余定理
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int a[4]={2,3,4679,35 ...
- interface 接口 和多态的含义
<?php //interface关键字用于定义接口 interface ICanEat{ //接口里面的方法不需要方法的实现 public function eat($food) ; } // ...
- return()函数
在函数中,执行完return()函数后,下面的语句就不会再执行了.例子: <?php function fn() { echo "you are awsome"; retur ...
- cocos2d-js 序列帧动画
var spriteCache = cc.spriteFrameCache;spriteCache.addSpriteFrames(res.fireworks_plist,res.fireworks_ ...
- docker学习(1)docker的安装
原文地址:http://blog.csdn.net/we_shell/article/details/38352837 1. 前言 Docker的英文本意是“搬运工”,在程序员的世界里,Docker搬 ...