P2214 [USACO14MAR]哞哞哞Mooo Moo

题目描述

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.

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

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

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

输入输出格式

输入格式:

  • 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.

输出格式:

  • Line 1: The minimum number of cows owned by FJ, or -1 if there is no

configuration of cows consistent with the input.

输入输出样例

输入样例#1: 复制

5 2
5
7
0
17
16
20
19

输出样例#1: 复制

4

说明

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.

Source: USACO 2014 March Contest, Silver

思路

  • 注意到声音只会从左往右传,那么就可以推出每一块真正发出的声音b[i]

$$b[i]=a[i]-max(a[i-1]-1,0)$$

  • 接下来就是,简单的完全背包

    • 把牛的叫声看作体积,把音量看作总容量,把牛的数量看作商品数

代码

#include<cmath>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#define re register int
using namespace std;
inline int read(){
int x=0,w=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') w=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x*w;
}
const int INF=2147483647;
int n,k,maxb=-1,ans=0;
int v[25],a[105],b[105],f[100005];
int main() {
freopen("p2214.in","r",stdin);
freopen("p2214.out","w",stdout);
n=read(),k=read();
for(re i=1;i<=k;++i) v[i]=read();
for(re i=1;i<=n;++i) a[i]=read();
for(re i=1;i<=n;++i) { b[i]=a[i]-max(a[i-1]-1,0); maxb=max(maxb,b[i]); }
for(re i=1;i<=maxb;++i) f[i]=INF;
for(re i=1;i<=k;++i) for(re j=v[i];j<=maxb;j++) { if(f[j-v[i]]!=INF) f[j]=min(f[j],f[j-v[i]]+1); }
for(re i=1;i<=n;i++) { if(f[b[i]]==INF) { printf("-1\n"); return 0;} else ans+=f[b[i]]; }
printf("%d\n",ans);
return 0;
}

【题解】Luogu P2214 [USACO14MAR]哞哞哞Mooo Moo的更多相关文章

  1. P2214 [USACO14MAR]哞哞哞Mooo Moo

    链接:Miku ---------------------- 这道题还是个背包 --------------------- 首先看一下声音的组成,对于每一个农场的声音,它是由两部分组成的 :上一个农场 ...

  2. USACO Mooo Moo

    洛谷 P2214 [USACO14MAR]哞哞哞Mooo Moo 洛谷传送门 JDOJ 2416: USACO 2014 Mar Silver 3.Mooo Moo JDOJ传送门 Descripti ...

  3. 【题解】Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo

    Luogu P3123 [USACO15OPEN]贝茜说哞Bessie Goes Moo 题目描述 Farmer John and Bessie the cow love to exchange ma ...

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

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

  5. [题解] Luogu P5446 [THUPC2018]绿绿和串串

    [题解] Luogu P5446 [THUPC2018]绿绿和串串 ·题目大意 定义一个翻转操作\(f(S_n)\),表示对于一个字符串\(S_n\), 有\(f(S)= \{S_1,S_2,..., ...

  6. 题解 Luogu P2499: [SDOI2012]象棋

    关于这道题, 我们可以发现移动顺序不会改变答案, 具体来说, 我们有以下引理成立: 对于一个移动过程中的任意一个移动, 若其到达的位置上有一个棋子, 则该方案要么不能将所有棋子移动到最终位置, 要么可 ...

  7. 题解 luogu P1144 【最短路计数】

    本蒟蒻也来发一次题解第一篇请见谅 这个题有几个要点 1.无向无权图,建图的时候别忘记建来回的有向边[因此WA掉1次 2.无权嘛,那么边长建成1就好了2333333 3.最短路采用迪杰斯特拉(别忘用堆优 ...

  8. 题解 Luogu P1110 【[ZJOI2007]报表统计】

    感谢 @cmy962085349 提供的hack数据,已经改对了. 先声明,我好像是题解里写双$fhq$ $treap$里唯一能过的...(最后两个点啊) 思路:首先看题目,$MIN_GAP_SORT ...

  9. 题解 Luogu P3370

    讲讲这题的几种做法: 暴力匹配法 rt,暴力匹配,即把字符串存起来一位一位判相等 时间复杂度$ O(n^2·m) $ 再看看数据范围 \(n\le10^5,m\le10^3\) 当场爆炸.当然有暴力分 ...

随机推荐

  1. 分布式ID

    需求 全局唯一 高性能 高可用 简单易用 UUID 优点: 唯一 不依赖于任何第三方服务 缺点: 是字符串类型而非数字,不满足数字ID的需求 字符串太长了,DB查询效率受影响 数据库自增ID 如果使用 ...

  2. 老J的技术分享之总结

    老J做IT这块有二十多个年头了,算是中国IT的见证者与参与者.那个时候刚开始接触和了解时,对于他的一些建议,我不是很乐于去接受,因为我觉得他的那一套技术体系不是很适合如今的情况,当时间久了后发现,他对 ...

  3. 关于flask的模板注入的学习

    flask模板注入的学习 关于flask模版注入,之前不太理解,看了很多文章才弄懂,主要原理就是渲染函数的参数用户可控就造成了模板注入 就会使用户构造恶意的代码进行逃逸从而进行攻击 flask模板渲染 ...

  4. 【转载】在Linux系统下用dd命令制作ISO镜像U盘启动盘

    #### 将U盘插入USB接口 #umount /dev/sdb* #dd if=/iso存放路径/XXX.iso of=/dev/sdb bs=1M ##### [转载]在Linux系统下用dd命令 ...

  5. C++知识点案例 笔记-4

    1.纯虚函数 2.抽象类 3.内部类 4.运算符重载 5.类的函数重载 6.友元的函数重载 1.纯虚函数 ==纯虚函数== //有时基类中无法给出函数的具体体现,定义纯虚函数可以为派生函数保留一个函数 ...

  6. Linux xargs命令-(转载)

    xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理.通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从 ...

  7. 自用Chrome插件推荐【附教程及下载】

    自用Chrome插件推荐[附教程及下载] 豆子   1,675 人赞同了该文章 2019.11.03更新 原文同步发布在我的个人博客 Chrome插件自用​blog.douzi.work 都是我自己一 ...

  8. java内部类与静态内部类对比

    内部类 静态内部类 有一个隐式引用,指向实例化这个对象的外部类对象 没有这个附加指针 不支持静态字段(language15) 支持哦 不支持静态方法 (language15) 支持哦 接口中的内部类自 ...

  9. opentack - 本地化

    目录 1 Openstack minimal component 1 组件与功能 2 集群数据存储 2 neutron控制端和计算节点 2.1 SDN网络实现方式 2.2 安全组实现 2.3 虚拟机内 ...

  10. 情景剧:C/C++中的未定义行为(undefined behavior)

    写在前面 本文尝试以情景剧的方式,轻松.直观地解释C/C++中未定义行为(undefined behavior)的概念.设计动机.优缺点等内容1,希望读者能够通过阅读本文,对undefined beh ...