P2858 [USACO06FEB]奶牛零食Treats for the Cows区间dp,级像矩阵取数, f[i][i+l]=max(f[i+1][i+l]+a[i]*(m-l),f[i][i+l-1]+a[i+l]*(m-l)); #include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<ctime&g…
P2858 [USACO06FEB]奶牛零食Treats for the Cows 区间dp 设$f[l][r]$为取区间$[l,r]$的最优解,蓝后倒着推 $f[l][r]=max(f[l+1][r]+a[l]*p,f[l][r-1]+a[r]*p)$ #include<iostream> #include<cstdio> #include<cstring> using namespace std; int max(int a,int b){return a>b…
P2858 [USACO06FEB]奶牛零食Treats for the Cows 题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period ti…
题目传送门 做完A Game以后找道区间dp练练手...结果这题没写出来(哭). 和A Game一样的性质,从两边取,但是竟然还有天数,鉴于之前做dp经常在状态中少保存一些东西,所以这次精心设计了状态(不对的). 开始的naive想法:设f[i][j][0/1]为把第1~i / i~n的零食兜售完所得的最多钱,然后我们就会发现这个状态十分难转移. 水题还看了题解. 十分朴素的区间dp,和我在能量项链总结的如出一辙. 本题突破口:可以直接通过区间来得出当前是第几天!! Code #include<…
题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many re…
https://www.luogu.org/problemnew/show/P2858 方程很好想,关键我多枚举了一次(不过也没多大关系) #include <bits/stdc++.h> #define read read() #define up(i,l,r) for(register int i = (l);i <= (r); i++) using namespace std; ; int n,a[N],f[N][N]; int read { ;char ch = getchar(…
[USACO06FEB]奶牛零食Treats for the Cows 思路: 区间DP: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 2005 #define ll long long ll n,ai[maxn],dp[maxn][maxn],sum[maxn]; inline void in(ll &now) { ; ')Cget=getchar(); ') { now=now*+Cget-'; Cg…
Description 约翰经常给产奶量高的奶牛发特殊津贴,于是很快奶牛们拥有了大笔不知该怎么花的钱.为此,约翰购置了N(1≤N≤2000)份美味的零食来卖给奶牛们.每天约翰售出一份零食.当然约翰希望这些零食全部售出后能得到最大的收益.这些零食有以下这些有趣的特性: •零食按照1..N编号,它们被排成一列放在一个很长的盒子里.盒子的两端都有开口,约翰每 天可以从盒子的任一端取出最外面的一个. •与美酒与好吃的奶酪相似,这些零食储存得越久就越好吃.当然,这样约翰就可以把它们卖出更高的价钱. •每份…
我是个傻逼,这么水的题都会T #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #define R(a,b,c) for(register int a = (b); a <= (c); ++ a) #define nR(a,b,c) for(register int a = (b); a >= (…
传送门 f[i][j][k] 表示 左右两段取到 i .... j 时,取 k 次的最优解 可以优化 k 其实等于 n - j + i 则 f[i][j] = max(f[i + 1][j] + a[i] * (n - j + i), f[i][j - 1] + a[j] * (n - j + i)) 边界 f[i][i] = a[i] * n 递推顺序不好求,所以选择记忆化搜索. ——代码 #include <cstdio> #include <iostream> ; int n…
我们可以记录头和尾再加一个卖了的零食数目,如果头超过尾就return 0. 如果遇到需要重复使用的数,(也就是不为零的d数组)就直接return d[tuo][wei]. 如果没有,就取卖头一个与最后一个的最大值,并记录下来. 代码也有注释,具体可以自己看. #include<bits/stdc++.h> using namespace std; int n,a[2001],d[2001][2001]; int dfs(int gong,int tuo,int wei) { if(tuo>…
写不来dp的日常 ....就这样吧 #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; ][],v[];//取了i个有j个在左 int n; int main() { int i,j,r; scanf("%d",&n); ;i<=n;i++)scanf("%d",&am…
题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many re…
题目描述 约翰经常给产奶量高的奶牛发特殊津贴,于是很快奶牛们拥有了大笔不知该怎么花的钱.为此,约翰购置了N(1≤N≤2000)份美味的零食来卖给奶牛们.每天约翰售出一份零食.当然约翰希望这些零食全部售出后能得到最大的收益.这些零食有以下这些有趣的特性: •零食按照 1...N 编号,它们被排成一列放在一个很长的盒子里.盒子的两端都有开口,约翰每天可以从盒子的任一端取出最外面的一个. •与美酒与好吃的奶酪相似,这些零食储存得越久就越好吃.当然,这样约翰就可以把它们卖出更高的价钱. •每份零食的初始…
题目链接 https://www.luogu.org/problemnew/show/P2858 一句话题意: https://cn.vjudge.net/problem/POJ-3186#author=Re0 分析 很显然这道题是不行滴,但是把这个数列看作从一个个区间倒着向外扩展取数而成的话,这样就保证了最优子结构和无后效性两个特点,于是就开始DP了 按照区间DP一贯的套路,先初始化元区间,也就是长度为1的区间值\(f[i][i]=a[i] * n\),为什么要倒着取呢?前面已经说明了,这样保…
[BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given…
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for…
Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for…
http://poj.org/problem?id=3186   Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time…
Treats for the Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7949   Accepted: 4217 Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per da…
题目链接  Treats for the Cows 直接区间DP就好了,用记忆化搜索是很方便的. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define rep(i,a,b) for(int i(a); i <= (b); ++i) #define LL long long + ; LL f[Q]…
https://www.luogu.org/problemnew/show/P2858 毫无疑问区间dp. ![区间dp入门] 我们定义dp[i][j]表示从i到j的最大收益,显然我们需要利用比较小的区间来推出更大的区间. 初始化dp[i][i]=单价,这里先不考虑第几天卖. 现在我们来确定小区间与大区间的关系,继而写出递推方程式. 每一个区间长度为一的块,想要扩大区间长度,那么只需要考虑对于现区间的左右端点的相邻点,我们可以通过比较确定是取左邻点还是右邻点(i,j分别表示左右端点). $$dp…
FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many reasons…
Treats for the Cows 先搬中文 Descriptions: 给你n个数字v(1),v(2),...,v(n-1),v(n),每次你可以取出最左端的数字或者取出最右端的数字,一共取n次取完.假设你第i次取的数字是x,你可以获得i*x的价值.你需要规划取数顺序,使获得的总价值之和最大. Input 第一行一个数字n(1<=n<=2000). 下面n行每行一个数字v(i).(1<=v(i)<=1000) Output 输出一个数字,表示最大总价值和. Sample In…
题目 这个题一开始能看出来是一道动态规划的题目,但是并不知道如何写状态转移方程,但是我们可以想一想这个题应该是一道区间DP,而区间DP的特点就是状态转移方程一般跟该区间的左节点和右节点或者中间断点有关,因为我们一次是从两个点中选一个而原题中的a值是(n-(left-right)),因此我们就可以得出状态转移方程 : dp[i][j]=max(dp[i][j-]+data[j]*(n-(j-i)),dp[i+][j]+data[i]*(n-(j-i))); 知道了这个就完了吗,当然不是,首先我们要…
题目链接: 点我 题目分析: 这是什么,区间dp吗?怎么大佬都在说区间dp的样子 完蛋区间dp都不知道是啥quq 于是使用了玄学的姿势A过了这道题 设dp[i][j][0]表示第i天,左边选了j个,当前选择了左边的最大价值方案,dp[i][j][1]表示从右边选 (其实第三维好像不用,但我还是记录了一下--这个思路和洛谷题解里面有一篇差不多,那个就是没记左右的,可以去看一下) 那么很容易得到状态转移方程: (其中a是题目所给数组) 最后ans=max(f[n][i][0],f[n][i][1])…
裸的区间dp,设f[i][j]为区间(i,j)的答案,转移是f[i][j]=max(f[i+1][j]+a[i](n-j+i),f[i][j-1]+a[j]*(n-j+i)); #include<iostream> #include<cstdio> using namespace std; const int N=2005; int n,a[N],f[N][N]; int main() { scanf("%d",&n); for(int i=1;i<…
记录一些基础的区间 \(\text{DP}\) 题. 0x00 AT_dp_n N - Slimes 最板的区间 \(\text{DP}\) . 记 \(f[i][j]\) 表示合并 \(i\sim j\) 区间的最小代价,初始化为 \(\text{inf}\),\(f[i][i]=0\) (一个数不需要代价). 第一维从小到大枚举区间长度 \(len\),第二维枚举左端点 \(i\),同时可以计算出右端点 \(j=i+len-1\),第三维枚举段点 \(k\),转移方程为 \[f[i][j]=…
dp( L , R ) = max( dp( L + 1 , R ) + V_L * ( n - R + L ) , dp( L , R - 1 ) + V_R * ( n - R + L ) ) 边界 : dp( i , i ) = V[ i ] * n -------------------------------------------------------------------------------------------- #include<cstdio> #include&l…
http://www.lydsy.com/JudgeOnline/problem.php?id=1652 dp.. 我们按间隔的时间分状态k,分别为1-n天 那么每对间隔为k的i和j.而我们假设i或者j在间隔时间内最后取.那么在这个间隔时间内最后取的时间就是n-k+1(这个自己想..也就是说,之前在n-(k-1)+1的时间间隔内取过了,现在我们要多了一个时刻,相当于取这个早了一个时间) 然后就是 k为阶段 i为左端点 j=i+k-1为右端点 t=n-k+1为i-j取最后一个的时间 然后转移 f[…