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 ...
随机推荐
- Spring基础篇——DI/IOC和AOP原理初识
DI(Dependency Injection),依赖注入,和我们常听说的另一个概念 IOC(控制反转)其实归根结底实现的功能是相同的,只是同样的功能站在不同的角度来阐述罢了.这里博主就不去过多的辨析 ...
- winform PictureBox图片上动态添加Label或其他控件
效果图: 代码: //分页或者多次加载时,需要删除之前产生的lable等控件 ; tabID < ; tabID++) { foreach (Control control in this.ta ...
- IO流的部分类简述
InputStream 类: InputStream 位于 java.io 包中,它是一个抽象类,表示字节输入流,需要定义 InputStream 子类的应用程序必须总是提供返回下一个输入字节的方法 ...
- tf 2.0
tf.function和Autograph使用指南-Part 1 "Keras之父发声:TF 2.0 + Keras 深度学习必知的12件事" Effective TensorFl ...
- Python内置函数系列
Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义. 作用域相关(2) locals() :以字典类型返回当前位置 ...
- vue-cli || webpack 打包的时候css里面写的背景图片的路径出错问题
.bg width 100% position fixed left 0 top 0 height 100vh z-index -1 background url('~@/assets/imgs/bg ...
- 基于OPENldap搭建postfix 虚拟用户
本文首发: https://www.somata.work/2019/DependOPENldapBuildPostfixVirtualMailUser.html postfix + dovecot ...
- PAT Basic 1077 互评成绩计算 (20 分)
在浙大的计算机专业课中,经常有互评分组报告这个环节.一个组上台介绍自己的工作,其他组在台下为其表现评分.最后这个组的互评成绩是这样计算的:所有其他组的评分中,去掉一个最高分和一个最低分,剩下的分数取平 ...
- How to resolve the 403 error when send POST request from Postman
Root cause: the site refused the connection from the http request origin, by default it is setted as ...
- No result defined for action com.java.test.Action.HelloAction and result index
Struts中配置action访问出错: Struts Problem Report Struts has detected an unhandled exception: Messages: No ...