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 ...
随机推荐
- 【arc077f】AtCoder Regular Contest 074 F - Lotus Leaves
题意 给定一个n*m的池塘,每个格子上可能有叶子. 从一个叶子出发,可以跳到相同行或相同列的叶子. 问至少去掉多少叶子,使得起点不能到达终点. \(n,m<=100\) 解法 很显然的最小割模型 ...
- mysql基础记录
1. 概念介绍 数据库:专门存储数据,存储数据的仓库,同时提供了对数据的操作方法,增删改查的方法 事务 事务:是作为一个单元的一组有序的数据库操作,如果组当中所有操作都成功,则事务执行成功,如果有一个 ...
- nginx 限制ip的请求频率
需要注意,这个很容易误伤 还需要结合别的办法进行共同配置 总是有人很清闲的扫我的网站 ,所以要限制一下了 先来看看没有限制的时候,并发10 100次 服务器一直在正常响应 限制一下,之后再测试一下 ...
- Caused by: java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
学习htmlutil发现报错 完整的引入 <!-- 引入htmlunit --> <dependency> <groupId>net.sourceforge.htm ...
- HTML:如何将网页分为上下两个部分
1.使用table: <table> <tr> <td height="80%"><jsp:include page=" ...
- 常用命令4-文件搜索命令 2- which
大家发现,cd 使用whereis和使用which都找不到他所在位置.是因为cd是linux的shell内置命令.那什么是shell,就是当前咱们操作界面.咱们看到的ls等命令都是通过外部安装的,所以 ...
- day18 11.复习
其实以前写的每条SQL语句都是有事务的,因为它默认的事务是autocommit=on(自动事务).mysql的autocommit是on,oracle的autocommit是off.
- nodeJs基础方法
Node.js 是一个基于Chrome javascript 运行时建立的一个平台, 用来方便地搭建快速的 易于扩展的网络应用 Node.js 借助事件驱动, 非阻塞I/O 模型变得轻量和高效, 非常 ...
- python之特点
.python区分大小写:2.注释规范:python使用井号#作为单行注释,且注释的位置,一般放在要注释代码的前一行或这代码的右侧:多行注释则可以用连续三个单引号开始一行,并连续三个单引号在要注释的代 ...
- Django项目:CRM(客户关系管理系统)--01--01PerfectCRM基本配置ADMIN01
一.CRM项目需求 二.CRM项目新建 PerfectCRM crm