POJ-3186_Treats for the Cows
Treats for the Cows
Time Limit: 1000MS Memory Limit: 65536K
Description
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 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
Line 1: A single integer, N
Lines 2..N+1: Line i+1 contains the value of treat v(i)
Output
Line 1: The maximum revenue FJ can achieve by selling the treats
Sample Input
5
1
3
1
5
2
Sample Output
43
Hint
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.
题意:给予一个数组,每次可以取前面的或者后面的,第k次取的v[i]价值为v[i]*k,问总价值最大是多少。
题解:一个区间DP题目,每一次取的时候可以由d[i+1][j]或者d[i][j-1]转移而来。
转移方程:dp[i][j]=max(dp[i+1][j]+p[i]*(n+i-j),dp[i][j-1]+p[j]*(n+i-j)); 其中n-(j-i)是第几次取。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = 2050;
int dp[maxn][maxn];
int main()
{
int n,v[maxn],i,j;
memset(dp,0,sizeof(dp));
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
dp[i][i]=p[i];
}
for(i=n;i>=1;i--)
for(j=i;j<=n;j++)
dp[i][j] = max(dp[i+1][j]+v[i]*(n-(j-i)),dp[i][j-1]+v[j]*(n-(j-i)));
printf("%d\n",dp[1][n]);
return 0;
}
POJ-3186_Treats for the Cows的更多相关文章
- POJ 2387 Til the Cows Come Home (图论,最短路径)
POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)
题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- POJ 2387 Til the Cows Come Home (最短路 dijkstra)
Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...
- (简单) POJ 2387 Til the Cows Come Home,Dijkstra。
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- POJ 2387 Til the Cows Come Home 【最短路SPFA】
Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...
- POJ 2456: Aggressive cows(二分,贪心)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20485 Accepted: 9719 ...
随机推荐
- Hosts 广告
# 百度 127.0.0.1 cpro.baidustatic.com 127.0.0.1 dup.baidustatic.com 127.0.0.1 hm.baidu.com 127.0.0.1 i ...
- 20190819 [ B ]-沫
这次考试很懵,于是我记录了考试过程. 这是B场,比较简单,A场比赛题解请去 下面直接展开=.= 考试过程: 先看三道题, T1,我一下就想到了内个等比数列.于是慌了,我当时是水果的. T2,没思路 T ...
- Eclipse配置Maven详细教程
一.使用eclipse自带的maven插件 首先,现在下载Eclipse Mars之后的版本,基本上都自带了maven插件,无需自己再安装maven. 有几个注意点: 1.默认的本地仓库的目录是在C: ...
- MySQL数据库的全局锁和表锁
1.概念 数据库锁设计的初衷是处理并发问题.作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则.而锁就是用来实现这些访问规则的重要数据结构. 2.锁的分类 根据加锁的范围, ...
- Struts_客户列表练习
1.导包 2.配置文件struts.xml 3.创建CustomerAction 4.修改menu.jsp和List.jsp 5.配置web.xml
- 一个基于swoole的作业调度组件,已经实现了redis和rabitmq队列消息存储。
https://github.com/kcloze/swoole-jobs 一个基于swoole的作业调度组件,已经实现了redis和rabitmq队列消息存储.参考资料:swoole https:/ ...
- Python要如何实现(列表)排序?
排序,是许多编程语言中经常出现的问题.同样的,在Python中,如何是实现排序呢?(以下排序都是基于列表来实现) 一.使用Python内置函数进行排序 Python中拥有内置函数实现排序,可以直接调用 ...
- 使用web-component搭建企业级组件库
组件库的现状 前端目前比较主流的框架有react,vuejs,angular等. 我们通常去搭建组件库的时候都是基于某一种框架去搭建,比如ant-design是基于react搭建的UI组件库,而ele ...
- ubuntu上安装字体
# fc-list # sudo apt-get -y install fontconfig xfonts-utils # sudo cp XXX.ttf /usr/shar ...
- 如何用KNIME进行情感分析
Customer Intelligence Social Media Finance Credit Scoring Manufacturing Pharma / Health Care Retail ...