POJ 3186 Treats for the Cows ——(DP)
第一眼感觉是贪心,,果断WA。然后又设计了一个两个方向的dp方法,虽然觉得有点不对,但是过了样例,交了一发,还是WA,不知道为什么不对= =,感觉是dp的挺有道理的,,代码如下(WA的):
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = + ; int a[N];
int dp[N][N];
int n; int getDay(int i,int j)
{
return n - (j - i) + ;
} int main()
{
while(scanf("%d",&n) == )
{
for(int i=;i<=n;i++) scanf("%d",a+i);
memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n+;j++) dp[i][j] = max(dp[i][j], dp[i-][j] + getDay(i,j) * a[i]);
}
for(int j=n+;j>=;j--)
{
for(int i=j-;i>=;i--) dp[i][j] = max(dp[i][j], dp[i][j+] + getDay(i,j) * a[j]);
}
int ans = ;
for(int i=;i<=n;i++) ans = max(ans, dp[i][i+]);
printf("%d\n",ans);
}
return ;
}
WA的DP
看了别人的博客以后,有一个不错的dp方法:dp[i][j]表示左边已经选了i个右边已经选了j个最大值,然后转移也很明显。AC代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int N = + ; int a[N];
int dp[N][N];
int n; int main()
{
while(scanf("%d",&n) == )
{
for(int i=;i<=n;i++) scanf("%d",a+i);
memset(dp,,sizeof dp);
for(int i=;i<=n;i++)
{
for(int j=;i+j<=n;j++)
{
if(i > ) dp[i][j] = max(dp[i][j], dp[i-][j] + a[i] * (i+j));
if(j > ) dp[i][j] = max(dp[i][j], dp[i][j-] + a[n-j+] * (i+j));
}
}
int ans = ;
for(int i=;i<=n;i++) ans = max(ans, dp[i][n-i]);
printf("%d\n",ans);
}
return ;
}
POJ 3186 Treats for the Cows ——(DP)的更多相关文章
- poj 3186 Treats for the Cows(区间dp)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- 【BZOJ】1652: [Usaco2006 Feb]Treats for the Cows(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1652 dp.. 我们按间隔的时间分状态k,分别为1-n天 那么每对间隔为k的i和j.而我们假设i或者 ...
- [luoguP2858] [USACO06FEB]奶牛零食Treats for the Cows(DP)
传送门 f[i][j][k] 表示 左右两段取到 i .... j 时,取 k 次的最优解 可以优化 k 其实等于 n - j + i 则 f[i][j] = max(f[i + 1][j] + a[ ...
- poj 3186 Treats for the Cows(dp)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- POJ 3186 Treats for the Cows (动态规划)
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...
- POJ 2182 / HDU 2711 Lost Cows(平衡树)
Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular di ...
- POJ 3186 Treats for the Cows 一个简单DP
DP[i][j]表示现在开头是i物品,结尾是j物品的最大值,最后扫一遍dp[1][1]-dp[n][n]就可得到答案了 稍微想一下,就可以, #include<iostream> #inc ...
- POJ 3186 Treats for the Cows
简单DP dp[i][j]表示的是i到j这段区间获得的a[i]*(j-i)+... ...+a[j-1]*(n-1)+a[j]*n最大值 那么[i,j]这个区间的最大值肯定是由[i+1,j]与[i,j ...
- POJ 3267:The Cow Lexicon(DP)
http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
随机推荐
- 【转载】在使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support
在使用JDBC连接MySql时报错:You must configure either the server or JDBC driver (via the serverTimezone config ...
- iframe滚动条充当浏览器滚动条
在做博客项目的时候,使用了iframe,方便根据选择的文章类别切换显示的内容,但是文章一般都有很多,通过bootstrap的媒体列表的方式显示的话,iframe是一定会出现滚动条的,特别是我添加了一个 ...
- kong命令(三)route
介绍 route 是一套匹配客户端请求的规则.每个route都会匹配一个service,每个service可定关联多个route. 可以说service:route=1:n.一对多的关系.每个匹配到r ...
- legacy
int bw = blockDim.x; int bh = blockDim.y; int tx = threadIdx.x%bw; int ty = threadIdx.y%bh; __shared ...
- php定界符介绍
php界定符就是为了照样输出内容.它的格式如下: <<<EOF ...... EOF; 其中EOF是自定义的变量,但要成对出现! 首先附上一段php代码: <?php $a = ...
- .Net给图片加水印,并解决“无法从带有索引像素格式的图像创建Graphics对象”问题
using (Image img = Image.FromFile(savePath)) { //如果原图片是索引像素格式之列的,则需要转换 if (img.PixelFormat!=null) { ...
- ifeq ifneq ifdef ifndef
条件语句中使用到了三个关键字:“ifeq”.“else”和“endif”.其中: 1. “ifeq”表示条件语句的开始,并指定了一个比较条件(相等).之后是用圆括号括包围的.使用逗号“, ...
- fiddle--APP弱网测试
一.安装Fiddler 网上说要先安装.NET Framwork4,应该是由于本机已装,所以在安装Fiddler时并没有相关提示. Fiddler安装包:https://www.telerik.com ...
- linux——运维基础,与常用命令
1 运维概述 1 什么是运维 服务器的运行维护 2 名词 IDC(互联网数据中心) 3 监控软件 zabbix(用的最多), nagios, cactti 4 常用的linux操作系统 1 CentO ...
- springboot中使用cache和redis
知识点:springboot中使用cache和redis (1)springboot中,整合了cache,我们只需要,在入口类上加 @EnableCaching 即可开启缓存 例如:在service层 ...