(区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)
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.
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define N 2005
#define met(a,b) (memset(a,b,sizeof(a))) int a[N], dp[N][N], n; int DFS(int L, int R, int k)
{
if(L>R || L< || R< || R>n || L>n) return -; if(dp[L][R]!=-)
return dp[L][R]; dp[L][R] = ;
dp[L][R] = max(DFS(L+, R, k+) + a[L]*k, DFS(L, R-, k+) + a[R]*k); return dp[L][R];
} int main()
{ while(scanf("%d", &n)!=EOF)
{
int i; met(dp, -);
met(a, ); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
dp[i][i] = a[i]*n; dp[][n] = DFS(, n, ); printf("%d\n", dp[][n]);
}
return ;
}
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
typedef long long LL;
#define N 2005
#define met(a,b) (memset(a,b,sizeof(a))) int a[N], dp[N][N], n; int main()
{ while(scanf("%d", &n)!=EOF)
{
int i, j, l; met(dp, );
met(a, ); for(i=; i<=n; i++)
scanf("%d", &a[i]); for(i=; i<=n; i++)
dp[i][i] = a[i]*n; for(l=; l<n; l++)
{
for(i=; i+l<=n; i++)
{
j = i+l;
dp[i][j] = max(dp[i+][j]+a[i]*(n-l), dp[i][j-]+a[j]*(n-l));
}
} printf("%d\n", dp[][n]);
}
return ;
}
(区间dp + 记忆化搜索)Treats for the Cows (POJ 3186)的更多相关文章
- UVA 10003 Cutting Sticks 区间DP+记忆化搜索
UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的 ...
- uva 10891 区间dp+记忆化搜索
https://vjudge.net/problem/UVA-10891 给定一个序列x,A和B依次取数,规则是每次只能从头或者尾部取走若干个数,A和B采取的策略使得自己取出的数尽量和最大,A是先手, ...
- loj 1031(区间dp+记忆化搜索)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1031 思路:dp[i][j]表示从区间i-j中能取得的最大值,然后就是枚举分割点了. ...
- BZOJ1055[HAOI2008]玩具取名 【区间dp + 记忆化搜索】
题目 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后 他会根据自己的喜好,将名字中任意一个字母用“WING”中任意两个字母代替,使得自己的名字能够 ...
- HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索
题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析: 枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...
- hdu 4597 Play Game(区间dp,记忆化搜索)
Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card ...
- poj 1088 滑雪(区间dp+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...
- Ural 1183 Brackets Sequence(区间DP+记忆化搜索)
题目地址:Ural 1183 最终把这题给A了.. .拖拉了好长时间,.. 自己想还是想不出来,正好紫书上有这题. d[i][j]为输入序列从下标i到下标j最少须要加多少括号才干成为合法序列.0< ...
- 洛谷1880 区间dp+记忆化搜索 合并石子
题目网址:https://www.luogu.com.cn/problem/P1880 题意是:给定一个序列,最小规则是相邻两个值的合并,开销是他们的和,将整个序列合并成一个值的情况下,求解该值的最小 ...
随机推荐
- Could not load file or assembly 'Microsoft.EntityFrameworkCore.Relational
提示的很明确了,缺少Microsoft.EntityFrameworkCore.Relational引用.nuget安装上即可.
- python 3.6.5 sys模块和os模块
1 sys.argv 命令行参数List,第一个元素是程序本身路径 2 sys.exit(n) 退出程序,正常退出时exit(0) 3 sys.version 获取Python解释程序的版本信息 4 ...
- c# 2016QQ自动登录程序
程序是抓QQ主程序窗体句柄,通过移位定位到QQ 输入框,虚拟键盘输入后,ALT切换到密码框的方式实现的 附程序: using System;using System.Collections.Gener ...
- Oracle_SQL(4) DDL 表和约束
数据库对象分为占存储空间的对象和不占存储存储空间的对象.占存储空间的对象主要包括:表.索引等.select distinct segment_type from dba_segments order ...
- L1-027 出租(20)(STL-map代码)
L1-027 出租(20 分) 下面是新浪微博上曾经很火的一张图: 一时间网上一片求救声,急问这个怎么破.其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2 ...
- do_something方法解析
/** * 运行任务 * @param $interval * @return bool */ static public function do_something($interval) { //是 ...
- Kali Linux 网络扫描秘籍
第三章 端口扫描(二) 作者:Justin Hutchens 译者:飞龙 协议:CC BY-NC-SA 4.0 3.6 Scapy 隐秘扫描 执行 TCP 端口扫描的一种方式就是执行一部分.目标端口上 ...
- 基于TCP的socket套接字的网络编程(客户端/服务端模式)
于数据完整性要求较高的场合,就应采用TCP协议. IP网络层提供IP寻址和路由.因为在网络上数据可以经由多条线路到达目的地,网络层负责找出最佳的传输线路. IP地址与数据包: IP层就是把数据分组从一 ...
- Linux下查看系统启动 、运行以及安装时间
1.uptime命令 例子显示已启动13天 当前用户一个 [root@localhost-live version-build-related]# uptime :: up days, :, use ...
- Scrapy框架——CrawlSpider类爬虫案例
Scrapy--CrawlSpider Scrapy框架中分两类爬虫,Spider类和CrawlSpider类. 此案例采用的是CrawlSpider类实现爬虫. 它是Spider的派生类,Spide ...