bzoj 1575: [Usaco2009 Jan]气象牛Baric【dp】
完了不会dp了
设f[i][j]为以i结尾,有j个时的最优值,辅助数组g[i][j]为s选了i和j,i~j中的误差值
转移是f[j][i]=min(f[k][i-1]+g[k][j])
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const long long N=105;
long long n,m,a[N],g[N][N],f[N][N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
for(int k=i+1;k<j;k++)
g[i][j]+=abs(a[i]+a[j]-2*a[k]);
for(int i=1;i<=n+1;i++)
for(int j=1;j<i;j++)
g[0][i]+=2*abs(a[j]-a[i]);
for(int i=0;i<=n;i++)
for(int j=i+1;j<=n;j++)
g[i][n+1]+=2*abs(a[j]-a[i]);
g[0][n+1]=1e18;
for(int i=0;i<=n+1;i++)
for(int j=0;j<=n+1;j++)
f[i][j]=1e18;
f[0][1]=0;
for(int i=2;i<=n+2;i++)
for(int j=i-1;j<=n+1;j++)
for(int k=0;k<j;k++)
f[j][i]=min(f[j][i],f[k][i-1]+g[k][j]);
for(int i=3;i<=n+2;++i)
if(f[n+1][i]<=m)
{
printf("%d %lld\n",i-2,f[n+1][i]);
return 0;
}
}
bzoj 1575: [Usaco2009 Jan]气象牛Baric【dp】的更多相关文章
- bzoj:1575: [Usaco2009 Jan]气象牛Baric
Description 为了研究农场的气候,Betsy帮助农夫John做了N(1 <= N <= 100)次气压测量并按顺序记录了结果M_1...M_N(1 <= M_i <= ...
- [BZOJ1575] [Usaco2009 Jan]气象牛Baric(DP)
传送门 DP f[i][j]表示前i个中选j个的最优解 预处理g[i][j]表示选i~j对答案的贡献 那么就可以n^3乱搞了! 注意边界 #include <cstdio> #includ ...
- bzoj1575 [Usaco2009 Jan]气象牛Baric
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1575 [题解] 动态规划,令f[i,j]表示前i个选了j个,且第i个必选的最小值. 转移就枚 ...
- 【动态规划】bzoj1575: [Usaco2009 Jan]气象牛Baric
预处理普通动态规划:庆祝1A三连 Description 为了研究农场的气候,Betsy帮助农夫John做了N(1 <= N <= 100)次气压测量并按顺序记录了结果M_1...M_N( ...
- [usaco jan 09] 气象牛 baric [dp]
题面: 传送门 思路: 题意有点绕,实际上就是给你一个计算规则,让你取最少的元素,通过这个计算方式,得到一个小于指定误差上限的结果 这个规则分为三个部分,这里分别用pre,sum,suf表示 因为给定 ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MB Submit: 665 Solved: 227[Sub ...
- BZOJ.1576.[Usaco2009 Jan]安全路经Travel(树形DP 并查集)
题目链接 BZOJ 洛谷 先求最短路树.考虑每一条非树边(u,v,len),设w=LCA(u,v),这条边会对w->v上的点x(x!=w)有dis[u]+dis[v]-dis[x]+len的距离 ...
- bzoj 1700: [Usaco2007 Jan]Problem Solving 解题 ——dp
Description 过去的日子里,农夫John的牛没有任何题目. 可是现在他们有题目,有很多的题目. 精确地说,他们有P (1 <= P <= 300) 道题目要做. 他们还离开了农场 ...
- BZOJ 1574: [Usaco2009 Jan]地震损坏Damage
Description 农夫John的农场遭受了一场地震.有一些牛棚遭到了损坏,但幸运地,所有牛棚间的路经都还能使用. FJ的农场有P(1 <= P <= 30,000)个牛棚,编号1.. ...
随机推荐
- Unity 3D 使用Relief Terrain Pack(RTP) 问题
Unity3D 5.2 RTP 3.2d -------------------------------------------------------------------- 使用RTP编译sha ...
- 升级到Offiec 2016后 Power View 不见了的处理方法
好吧 并不是没有了,而只是快捷方式需要手动的调整出来, 过程还是挺复杂,给一个官方文档吧. Turn on Power View in Excel 2016 for Windows https://s ...
- 确定协议-通过分析系统阶段需要知道该系统能不能进行性能测试-Omnipeek
- Leetcode 143.重排链表
重排链表 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示 ...
- hihoCoder#1109 最小生成树三·堆优化的Prim算法
原题地址 坑了我好久...提交总是WA,找了个AC代码,然后做同步随机数据diff测试,结果发现数据量小的时候,测试几十万组随机数据都没问题,但是数据量大了以后就会不同,思前想后就是不知道算法写得有什 ...
- POJ3352-Road Construction(边连通分量)
It's almost summer time, and that means that it's almost summer construction time! This year, the go ...
- Jquery判断某个字符串是否在数组中
使用$.inArray方法判断,如果存在则返回0,不存在返回-1,结果如下: 另外也可以将数组转为字符串,并使用正则表达式处理
- HDU4325 树状数组+离散化
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 Flowers Time Limit: 4000/2000 MS (Java/Others) ...
- Mayor's posters POJ - 2528
The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...
- Pagodas 等差数列
nn pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, ...