POJ - 3186 Treats for the Cows DP
传送门
乍一看与cf的某道题很相似,做了半天的贪心,然后是个二维dp。
这道题难的点在于你无法从最先取的值推到最后取的值,只能从内往外推。如果能看出来这点,其实就不难,但是很难想。
一开始dp[i][i]记录以该点为最后取的数,然后将长度dp到2,3~n.
k为长度,i,j为左右端点,判断将左右端点那个先后拿。
递推式 dp[i][j]=max(dp[i][j-1]+(n-k)*a[j],dp[i+1][j]+(n-k)*a[i]);
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define lowbit(x) x&(-x)
#define min4(a,b,c,d) min(min(a,b),min(c,d))
#define min3(x,y,z) min(min(x,y),min(y,z))
#define max4(a,b,c,d) max(max(a,b),max(c,d))
#define max3(x,y,z) max(max(x,y),max(y,z))
typedef long long ll;
const int inff=0x3f3f3f3f;
const double eqs=1e-9;
const double E=2.718281828459;
const double pi=acos(-1.0);
using namespace std;
const int maxn=2005;
int a[maxn];
int dp[maxn][maxn];
int main()
{int n;while(cin>>n){for(int i=1;i<=n;i++)scanf("%d",&a[i]);memset(dp,0,sizeof(dp));for(int i=1;i<=n;i++)dp[i][i]=a[i]*n;//最后拿的值for(int k=1;k<n;k++)for(int i=1;i+k<=n;i++){int j=i+k;//i为左边,j为右边,看取那个更大点dp[i][j]=max(dp[i][j-1]+(n-k)*a[j],dp[i+1][j]+(n-k)*a[i]);}printf("%d\n",dp[1][n]);}return 0;
}
POJ - 3186 Treats for the Cows DP相关推荐
- POJ 3186 Treats for the Cows dp
给你n个数 每一次只取一个数 只能前后两个方向取 第i次取出数的价值为自身价值*i 问最大价值为多少 #include<cstdio> #include<cstrin ...
- poj 3186 Treats for the Cows (区间dp)
题意:给你一个序列,每次只能从头或为取数,然后乘以这是第几个数,最后加和,是加和最大 思路:假设长度最开始是1,然后依次枚举长度,以及起点,dp[i][j]是又里面的两端点扩出来的(ps:代码不是这么 ...
- 动态规划训练20 [Treats for the Cows POJ - 3186 ]
Treats for the Cows POJ - 3186 简单的区间DP,就不解释了. #include<iostream> #include<cstdio> using ...
- Treats for the Cows
问题描述 Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money f ...
- P2858 [USACO06FEB]Treats for the Cows G/S 题解
emmmmmm,第二篇文章,多多写文章,好好掌握知识! 前言 原本在educoder上刷题,刷到[粉刷匠]一题,使用区间DP来做的.自己之前曾经小部分刷过背包DP的题目,对于区间DP还是知之甚少.在稍 ...
- [USACO06FEB]Treats for the Cows G/S奶牛零食(区间dp)
洛谷 acwing #include <bits/stdc++.h> #include <iostream> #include <cstring> #include ...
- poj3186 Treats for the Cows(区间)
题目链接:http://poj.org/problem?id=3186 题意:第一个数是N,接下来N个数,每次只能从队列的首或者尾取出元素. ans=每次取出的值*出列的序号.求ans的最大值. 样例 ...
- poj 3254 Corn Fields (状态压缩DP)
题目:http://poj.org/problem?id=3254 思路见代码: #include<iostream> using namespace std;const int MOD= ...
- POJ 1185 炮兵阵地(状态压缩DP)
Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原(用&quo ...
最新文章
- 中国肠道大会 | 3天日程,220+报告,肠道新知一次听够!
- what is success?
- 网络营销前夕怎样确定网站定位?这是网络营销开展的基础!
- 1.DBCP和CP30的区别
- rust模组服如何切换标准服_送给玩模组服的萌新们
- 计算机学院考勤管理办法,计科学院进一步加强课堂考勤实施意见(试行)
- Android 自动换行流式布局的RadioGroup
- python自动化办公入门-[Python] 自动化办公 docx操作Word基础代码
- 25. Consider support for a non-throwing swap
- 计算机操作系统---高响应比调度算法,操作系统最高响应比优先调度算法实验报告...
- 快递100码json
- 计算机毕业设计java+jsp旅游网站(源码+系统+mysql数据库+Lw文档)
- 下载苹果APP历史版本
- 让游戏在英国取得成功的 5 个技巧
- 利用Python实现图片信息隐藏
- 批处理---findstr命令详解
- nova3安装android10系统,一招搞定安卓机“久用必卡”顽疾:华为nova3i配备6GB超大运存...
- Rasa 使用ResponseSelector实现FAQ和闲聊
- 连用形,连体型 详解
- 微服务契约测试-Pact