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 ...
随机推荐
- angular之增删改查
<!DOCTYPE html> <html lang="en" ng-app="app"> <head> <meta ...
- java 多线程系列基础篇(六)之线程让步
1. yield()介绍 yield()的作用是让步.它能让当前线程由“运行状态”进入到“就绪状态”,从而让其它具有相同优先级的等待线程获取执行权:但是,并不能保证在当前线程调用yield()之后,其 ...
- ORACLE——日期时间格式化参数详解 之三
2.20 Y,YYY 返回有逗号分隔显示的年 SQL> select to_char(SYSTIMESTAMP,'Y,YYY') from dual; TO_CHAR(SYSTIMESTAMP, ...
- mysql 打印随机数
select rand(); 这样取出来的数据是类似这样的: 0.5389902438400223 要几位自己取几位: 取得方法类似 select substr(concat("000000 ...
- Eclipse中如何开启断言(Assert),方法有二
Eclipse中如何开启断言(Assert),方法有二:1.Run -> Run Configurations -> Arguments页签 -> VM arguments文本框中加 ...
- python中not的用法
python中的not具体表示是什么: 在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法: ...
- 关联查询 join的使用
#!/usr/bin/env python import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.dec ...
- css背景图片位置:background的position(转)
css背景图片位置:background的position position的两个参数:水平方向的位置,垂直方向的位置----------该位置是指背景图片相对于前景对象的 1.backgroun ...
- ArcGIS Field Type /esriFieldTypeDate(转)
ArcGIS Field Type The following table outlines the equivalent field data types in ArcCatalog, ArcO ...
- C语言中的static的作用?
在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. (1)第一个作用:隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有全局可见性.为理解这句话 ...