第一眼感觉是贪心,,果断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)的更多相关文章

  1. 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 ...

  2. 【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或者 ...

  3. [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[ ...

  4. 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 ...

  5. POJ 3186 Treats for the Cows (动态规划)

    Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for gi ...

  6. 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 ...

  7. POJ 3186 Treats for the Cows 一个简单DP

    DP[i][j]表示现在开头是i物品,结尾是j物品的最大值,最后扫一遍dp[1][1]-dp[n][n]就可得到答案了 稍微想一下,就可以, #include<iostream> #inc ...

  8. 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 ...

  9. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. Mysql的实现原理

    上篇文章已经简单介绍了Mysql索引的基本介绍,这篇文章主要讲解一下所以的实现原理.索引的定义其实非常复杂,严格的定义需要用到关系代数的概念,不在咱们讨论范围内,这里咱们只讨论mysql的常用的引擎的 ...

  2. HTTP协议探究(五):H2中一些重要的概念

    一 复习与目标 1 复习 简单密码学.对称加密与非对称加密 数字签名.数字证书 SSL/TLS HTTPS = HTTP + SSL/TLS,SSL/TLS为HTTP提供了保密性.完整性和鉴别性 2 ...

  3. C#项目中窗体的ShowDialog()和show()的区别

    ShowDialog()弹出的窗体为模式化窗体: show()弹出的窗体为非模式化窗体: 模式化窗体与非模式化窗体的区别: 模式化窗体会使程序中断,直到关闭窗体: 打开窗体后不能替换到其他窗体: 子窗 ...

  4. C#强制回收垃圾

    [DllImport("psapi.dll")] private static extern int EmptyWorkingSet(int hProcess); public v ...

  5. MySQL5.6.11安装步骤(Windows7 64位)

    1. 下载MySQL Community Server 5.6.21,注意选择系统类型(32位/64位) 2. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下. 3. 添加环境变量 ...

  6. 自定义centos

    目录 自定义centos 1. 为什么要自定义centos 2. 自定义centos步骤 自定义centos 1. 为什么要自定义centos 在使用官网的 centos镜像,只有200m,很小,但是 ...

  7. c#NPOI导出2007版本excel

    2003和2007版本区别: HSSFWorkbook(2003) IWorkbook(2007版本) 写完之后会有个问题,导出会报错[流已关闭]. NPOI生产.xlsx文件件时,在使用book.W ...

  8. string leetcode-6.ZigZag

    6. ZigZag Conversion 题面 The string "PAYPALISHIRING" is written in a zigzag pattern on a gi ...

  9. 面试总结关于Spring面试问题(精选)

    1.什么是Spring? Spring是一个反转控制IOC和AOP的开发框架和平台. 2.解释一下Spring? 轻量 : Spring 在大小和透明度上是轻量的,Spring基本核心版本大概只有1M ...

  10. 第五章、drf-JWT认证

    目录 JWT认证 JWT认证方式与其他认证方式对比: 优点 格式 drf - jwt 插件 官网 安装 token的签发与校验初识: 签发token 校验token 自定义drf-jwt配置 sett ...