BIT2014级软件学院程序设计-22 Treats for the Cows
来源:互联网 发布:人工智能 大数据 编辑:程序博客网 时间:2024/06/05 17:45
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 first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.
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.
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)
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
513152
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).
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.
也是一道DP的题。每次从头取或者从后取,DP方程也很容易。
#pragma comment(linker, "/STACK:102400000,102400000")#include<stdio.h>#include<string.h>#include<stdlib.h>#define maxn 2500int dp[maxn][maxn];int num[maxn];int max(int a, int b){if (a > b)return a;return b;}int main(){int n,i,j,flag;scanf("%d",&n);for (i = 1; i <= n; i++)scanf("%d", &num[i]);for (i = 1;i <= n;i++)dp[i][i] = num[i] * n;for (i = 1; i < n;i++)for (j = 1; j + i <= n; j++){flag = i + j;dp[j][flag] = max(dp[j + 1][flag] + (n - i)*num[j], dp[j][flag - 1] + (n - i)*num[flag]);}printf("%d\n", dp[1][n]);}
0 0
- BIT2014级软件学院程序设计-22 Treats for the Cows
- BIT2014级软件学院程序设计-08传送带
- BIT2014级软件学院程序设计-11 条形码
- BIT2014级软件学院程序设计-16 魔方
- BIT2014级软件学院程序设计-17 迷宫
- Treats for the Cows
- BIT2014级软件学院程序设计-01球体问题
- BIT2014级软件学院程序设计-02进制转换
- BIT2014级软件学院程序设计-03 Flip Game
- BIT2014级软件学院程序设计-04合并果子
- BIT2014级软件学院程序设计-05 修剪草坪
- BIT2014级软件学院程序设计-06贪婪的你
- BIT2014级软件学院程序设计-07 Crossing River
- BIT2014级软件学院程序设计-09 Easier Done Than Said
- BIT2014级软件学院程序设计-10 ⑨的统计
- BIT2014级软件学院程序设计-12 编程珠玑
- BIT2014级软件学院程序设计-13 平面上的邮局
- BIT2014级软件学院程序设计-14 比赛记分问题
- java内存泄露问题详解
- Ubuntu15.10安装XAMPP
- 数据库 三范式最简单最易记的解释
- ListView getItemType() && getItemTypeCount()
- 【Android】android-ndk-r10环境搭建
- BIT2014级软件学院程序设计-22 Treats for the Cows
- CListCtrl种InsertColumn不显示
- 怎么看Android官网,怎么看Google官网?可以通过设置Hosts来解决
- iOS批量上传图片(多图上传)
- 【程序员在法国】如何赢得老外的尊重?
- C# excel文件读取
- 字符串编码转换
- 配置Tomcat使用https协议(配置SSL协议)
- android 从微信分享的网页中启动APP