洛谷 P2214 [USACO14MAR]哞哞哞Mooo Moo

洛谷传送门

JDOJ 2416: USACO 2014 Mar Silver 3.Mooo Moo

JDOJ传送门

Description

Problem 3: Mooo Moo [silver] [Brian Dean, 2014]

Farmer John has completely forgotten how many cows he owns! He is too
embarrassed to go to his fields to count the cows, since he doesn't want
the cows to realize his mental lapse. Instead, he decides to count his
cows secretly by planting microphones in the fields in which his cows tend
to gather, figuring that he can determine the number of cows from the total
volume of all the mooing he hears.

FJ's N fields (1 <= N <= 100) are all arranged in a line along a long
straight road. Each field might contain several types of cows; FJ
owns cows that come from B different breeds (1 <= B <= 20), and a cow
of breed i moos at a volume of V(i) (1 <= V(i) <= 100). Moreover,
there is a strong wind blowing down the road, which carries the sound
of mooing in one direction from left to right: if the volume of mooing
in some field is X, then in the next field this will contribute X-1 to
the total mooing volume (and X-2 in the field after that, etc.).
Otherwise stated, the mooing volume in a field is the sum of the
contribution due to cows in that field, plus X-1, where X is the total
mooing volume in the preceding field.

Given the volume of mooing that FJ records in each field, please compute
the minimum possible number of cows FJ might own.

The volume FJ records in any field is at most 100,000.

Input

* Line 1: The integers N and B.

* Lines 2..1+B: Line i+1 contains the integer V(i).

* Lines 2+B..1+B+N: Line 1+B+i contains the total volume of all mooing
in field i.

Output

* Line 1: The minimum number of cows owned by FJ, or -1 if there is no
configuration of cows consistent with the input.

Sample Input

5 2 5 7 0 17 16 20 19

Sample Output

4

HINT

INPUT DETAILS:

FJ owns 5 fields, with mooing volumes 0,17,16,20,19. There are two breeds
of cows; the first moos at a volume of 5, and the other at a volume of 7.

OUTPUT DETAILS:

There are 2 cows of breed #1 and 1 cow of breed #2 in field 2, and there is
another cow of breed #1 in field 4.

题目翻译:

约翰忘记了他到底有多少头牛,他希望通过收集牛叫声的音量来计算牛的数量。

他的N (1 <= N <= 100)个农场分布在一条直线上,每个农场可能包含B (1 <= B <= 20)个品种的牛,一头品种i的牛的音量是V(i) ,(1 <= V(i) <= 100)。一阵大风将牛的叫声从左往右传递,如果某个农场的总音量是X,那么将传递X-1的音量到右边的下一个农场。另外,一个农场的总音量等于该农场的牛产生的音量加上从上一个农场传递过来的音量(即X-1)。任意一个农场的总音量不超过100000。

请计算出最少可能的牛的数量。

题解:

完全背包问题的一个小变形。

设置dp[i] 为音量为i时最少的奶牛数量,所以最后的答案就是所有牧场自己的音量的总和。

注意,是实际音量,不是实际音量。

所以在原有a数组保存农场总音量的同时,我们引入了b数组来保存这个牧场的单纯音量

注意,这里统计b数组的时候,一定要加max(和0比较),否则会WA3个点。

然后用maxx统计最大值,这样初始化的时候可以节省一点点时间。

然后就是振奋人心的DP过程啦!套用完全背包的模板的时候注意要加一个判断。

这个判断就是一种优化,emm,怎么说呢》自己体会吧!。

AC CODE:

#include<cstdio>
#include<algorithm>
using namespace std;
const int INF=1e9;
int n,B,maxx=-1,ans=0;
int v[25],a[105],b[105],dp[100005];
int main()
{scanf("%d%d",&n,&B);for(int i=1;i<=B;i++)scanf("%d",&v[i]);for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++){b[i]=a[i]-max(a[i-1]-1,0);maxx=max(maxx,b[i]);}for(int i=1;i<=maxx;i++)dp[i]=INF;for(int i=1;i<=B;i++)for(int j=v[i];j<=maxx;j++)if(dp[j-v[i]]!=INF)dp[j]=min(dp[j],dp[j-v[i]]+1);for(int i=1;i<=n;i++){if(dp[b[i]]==INF){printf("-1");return 0;}ans+=dp[b[i]];}printf("%d",ans);return 0;
}

转载于:https://www.cnblogs.com/fusiwei/p/11293722.html

USACO Mooo Moo相关推荐

  1. 【题解】Luogu P2214 [USACO14MAR]哞哞哞Mooo Moo

    P2214 [USACO14MAR]哞哞哞Mooo Moo 题目描述 Farmer John has completely forgotten how many cows he owns! He is ...

  2. (寒假集训)Mooo Moo (完全背包)

    Mooo Moo 时间限制: 1 Sec  内存限制: 64 MB 提交: 5  解决: 4 [提交][状态][讨论版] 题目描述 Farmer John has completely forgott ...

  3. jzoj 3812 Mooo Moo

    Description FJ 已经完全忘记了他有多少头奶牛!但是,跑到他的草场里数奶牛是一件很尴尬的事情,因为他不想让奶牛们知道他记忆有问题.作为替代,他决定秘密地把麦克风种在奶牛们通常聚集的草场里, ...

  4. 洛谷2214 哞哞哞Mooo Moo dp

    题目大意 农民约翰忘记了他到底有多少头牛,他希望通过收集牛叫声的音量来计算牛的数量. 他的N (1 <= N <= 100)个农场分布在一条直线上,每个农场可能包含B (1 <= B ...

  5. P2214 [USACO14MAR]哞哞哞Mooo Moo

    一道不错的背包题. 首先,只考虑这个单点,我们一定是希望用最少的牛的组合来完成这个点的音量.因为有用的信息只是音量,而对于这个音量是怎么组合出来的,其实是无所谓的.所以,我们要每一个单点提供的音量的所 ...

  6. 洛谷P2214 [USACO14MAR]哞哞哞Mooo Moo(完全背包)

    设a[i]为第i个农场的总音量,如果我们将a[i]减去a[i-1]-1(前提是a[i-1]不为0),设最终为b[i],就不难发现,其实就相当于完全背包问题,要我们用B种物品去,以最少的数量去填满容量为 ...

  7. Mooo Moo题解

    一眼看出是完全背包 设f[i]表示哞哞叫总音量为i时最少需要几头奶牛 a[i]为在第i个草场听到的哞哞叫总音量 则第i个草场自己的哞哞叫音量为a[i]-a[i-1]-1 代码呼之欲出 uses mat ...

  8. [背包DP] 洛谷相关题目整理与练习(74题-)

    题目 以背包为标签,搜出了这么多题,按难度排序,一道一道做: (*):下面有提到 TODO 题目 难度 备忘录 AC 采药 普及- 01背包模板 AC 开心的金明 普及- 01背包模板 AC 小A点菜 ...

  9. 【备战NOIP】专题复习1-动态规划-背包问题

    [备战NOIP]专题复习1-动态规划-背包问题 在阅读本文之前,建议先阅读背包九讲.本文通过相关的题目来讨论一些常见的背包套路,其中包括,01背包的模板以及应用,完全背包的模板以及应用,多重背包的模板 ...

最新文章

  1. SAP S/4 HANA中的供应链计划提升
  2. 吴恩达机器学习笔记55-异常检测算法的特征选择(Choosing What Features to Use of Anomaly Detection)
  3. 005_HttpServlet
  4. 火爆数据圈的数据分析工具,快速上手动态报表就是这么简单
  5. springboot添加swagger2组件
  6. IT人士运动方式选择建议
  7. AndroidStudio 3.4更新了啥?(转载)
  8. PHP复杂度,php 常用算法和时间复杂度
  9. (85)Vivado 多周期路径约束情况
  10. 一种解决Android studio 3.0 Build报错的方法
  11. X5045的C语言源码,X5045看门狗的单片机源程序和Proteus仿真原理图
  12. c语言 同时显示正切和余切函数,正切余切函数曲线辅助作图器及其使用方法
  13. 香港服务器托管单线路、双线路以及多线路如何区别
  14. Ubuntu18.04 打不开系统蓝牙适配器,也连接不上任何蓝牙设备Bug解决方案
  15. 各类dp的总结+例题
  16. 水晶报表中几种交叉表的实现方法 (作者阿泰)
  17. comet 简单了解
  18. https://ipcrs.pbccrc.org.cn/
  19. 真无线蓝牙耳机哪个品牌好?2023年真无线降噪耳机盘点
  20. Audacity分析清音浊音爆破音的时域及频域特性

热门文章

  1. 用js计算12个月的社保缴纳总额-企业信息公示
  2. 设计模式学习笔记--享元(Flyweight)模式
  3. Unity 解析视频流数据
  4. db db2_monitorTool IBM Rational Performace Tester
  5. PLC转换32位IEEE 754格式modbus 值到浮点
  6. chrome 前端开发插件:尺子
  7. 解决Cornerston的Authentication provider raised an exception (first credentials)问题
  8. android根据url加载图片路径,初学Android——通过URL加载图片
  9. python中可选参数是什么意思_什么是python的必选参数
  10. 编辑为什么建议转投_编辑建议转投其他期刊一般有哪些原因