[luogup2858][usaco06feb]奶牛零食treatsforthecows(dp)

蒟蒻zht的博客 蒟蒻zht的博客     2022-09-01     559

关键词:

传送门

 

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

递推顺序不好求,所以选择记忆化搜索。

 

——代码

技术分享
 1 #include <cstdio>
 2 #include <iostream>
 3 
 4 const int MAXN = 2001;
 5 int n;
 6 int a[MAXN], f[MAXN][MAXN];
 7 
 8 inline int read()
 9 {
10     int x = 0, f = 1;
11     char ch = getchar();
12     for(; !isdigit(ch); ch = getchar()) if(ch == -) f = -1;
13     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - 0;
14     return x * f;
15 }
16 
17 inline int max(int x, int y)
18 {
19     return x > y ? x : y;
20 }
21 
22 inline int dp(int x, int y)
23 {
24     if(f[x][y]) return f[x][y];
25     if(x == y) return f[x][y] = a[x] * n;
26     else return f[x][y] = max(dp(x + 1, y) + a[x] * (n - y + x), dp(x, y - 1) + a[y] * (n - y + x));
27 }
28 
29 int main()
30 {
31     int i;
32     n = read();
33     for(i = 1; i <= n; i++) a[i] = read();
34     printf("%d
", dp(1, n));
35     return 0;                                                                                                                                                                                                                                                                                                                
36 }
View Code

 

洛谷p2858[usaco06feb]奶牛零食treatsforthecows

题目描述FJhaspurchasedN(1<=N<=2000)yummytreatsforthecowswhogetmoneyforgivingvastamountsofmilk.FJsellsonetreatperdayandwantstomaximizethemoneyhereceivesoveragivenperiodtime.Thetreatsareinterestingfor 查看详情

p2858[usaco06feb]奶牛零食treatsforthecows

P2858[USACO06FEB]奶牛零食TreatsfortheCows区间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));1#include<iostream>2#include<cstdio>3#include<queue>4#include<algor 查看详情

ac日记——[usaco06feb]奶牛零食treatsforthecows洛谷p2858

[USACO06FEB]奶牛零食TreatsfortheCows 思路:  区间DP; 代码:#include<bits/stdc++.h>usingnamespacestd;#definemaxn2005#definelllonglonglln,ai[maxn],dp[maxn][maxn],sum[maxn];inlinevoidin(ll&now){c 查看详情

洛谷p2858·动态规划[usaco06feb]奶牛零食treatsforthecows

题面题目描述FJhaspurchasedN(1<=N<=2000)yummytreatsforthecowswhogetmoneyforgivingvastamountsofmilk.FJsellsonetreatperdayandwantstomaximizethemoneyhereceivesoveragivenperiodtime.Thetreatsareinterestingf 查看详情

[luogup1118][usaco06feb]数字三角形(代码片段)

题面题目描述FJandhiscowsenjoyplayingamentalgame.Theywritedownthenumbersfrom(1)to$N(1leNle10)$inacertainorderandthensumadjacentnumberstoproduceanewlistwithonefewernumber.Theyrepeatthisuntilonlyasinglenumberisleft.Forexample,oneinstanceofthegame(when(N=4))mightgolikethis:31244367916BehindFJ‘sb... 查看详情

p2859[usaco06feb]stallreservationss(区间问题,堆)(代码片段)

题目描述约翰的N(l<N<50000)头奶牛实在是太难伺候了,她们甚至有自己独特的产奶时段.当然对于某一头奶牛,她每天的产奶时段是固定的,为时间段A到B包括时间段A和时间段B.显然,约翰必须开发一个调控系统... 查看详情

p2859[usaco06feb]stallreservationss(区间问题,堆)(代码片段)

题目描述约翰的N(l<N<50000)头奶牛实在是太难伺候了,她们甚至有自己独特的产奶时段.当然对于某一头奶牛,她每天的产奶时段是固定的,为时间段A到B包括时间段A和时间段B.显然,约翰必须开发一个调控系统... 查看详情

luogup1821[usaco07feb]银牛派对silvercowparty

P1821[USACO07FEB]银牛派对SilverCowParty题目描述OnecowfromeachofNfarms(1≤N≤1000)convenientlynumbered1..Nisgoingtoattendthebigcowpartytobeheldatfarm#X(1≤X≤N).AtotalofM(1≤M≤100,000)unidirection 查看详情

luogup3121[usaco15feb]审查(黄金)censoring(gold)(代码片段)

题目描述FarmerJohnhaspurchasedasubscriptiontoGoodHooveskeepingmagazineforhiscows,sotheyhaveplentyofmaterialtoreadwhilewaitingaroundinthebarnduringmilkingsessions.Unfortunately,thelatestissuecontainsarathe 查看详情

luogup1821[usaco07feb]银牛派对silvercowparty

题目描述OnecowfromeachofNfarms(1≤N≤1000)convenientlynumbered1..Nisgoingtoattendthebigcowpartytobeheldatfarm#X(1≤X≤N).AtotalofM(1≤M≤100,000)unidirectional(one-wayroadsconnectspairsoffarms 查看详情

题解luogup2875[usaco07feb]牛的词汇thecowlexicon(代码片段)

题目描述FewknowthatthecowshavetheirowndictionarywithW(1≤W≤600)words,eachcontainingnomore25ofthecharacters‘a’..’z’.Theircowmunicationsystem,basedonmooing,isnotveryaccurate;som 查看详情

luogup3014[usaco11feb]牛线cowline(代码片段)

康拓展开--正运算会逆运算竟然忘了怎么写--晕死。#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constintmaxn=25;lla[maxn],jie[maxn];intmain()intn,m;scanf("%d%d",&n,&m);jie[0]=1;for(inti=1;i<=n;+ 查看详情

第一个五年计划

....学习模拟退火,自己再打几遍.4.没事多练DP题.完成情况6.18luoguP2858[USACO06FEB]奶牛零食TreatsfortheCows很暴力的DPP2016战略游戏基础的树形DP6.19luoguP1541乌龟棋一直都没做出来的一道DP题,今天终于解决了.P5035金坷垃傻逼的数论题P 查看详情

[luogup2875][usaco07feb]牛的词汇thecowlexicon(dp)

传送门 f[i]表示前i个字符去掉多少个的最优解直接暴力DP ——代码1#include<cstdio>2#include<cstring>3#include<iostream>45intn,m,cnt,f[301];6chars[301],a[601][26];78inlineintread()9{10intx=0, 查看详情

luogup2939[usaco09feb]改造路revampingtrails

约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体.通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高速公路.在高速公路上的通行几乎... 查看详情

[luogup2896][usaco08feb]一起吃饭eatingtogether(dp)

传送门 由于Di只有3种情况,那么就很简单了 f[i][j][0]表示前i个,且第i个变成j的递增序列最小修改次数f[i][j][1]表示前i个,且第i个变成j的递减序列最小修改次数状态转移看代码。 ——代码1#include<cstdio>2#incl... 查看详情

luogup2984[usaco10feb]给巧克力chocolategiving题解(代码片段)

题目链接:https://www.luogu.org/problemnew/show/P2984练习SPFA,把FJ当做起点,求出到所有牛的最短路,再把两个牛的相加。1#include<cstdio>2#include<queue>3#include<iostream>4#include<algorithm>5#include<cstring& 查看详情

「luogup3137」x「usaco16feb」圆形谷仓(代码片段)

#题目大意管理大大给修下$ extMarkdown$吧,严重影响做题体验啊。这道题的意思很简单就是给你一个长度是$n$的环,这个环上不均匀的分布着$n$头奶牛。一头奶牛移动要花费的代价是距离的平方,现在让你求出使得每个点上都有一... 查看详情