洛谷 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;
}

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

  3. P2214 [USACO14MAR]哞哞哞Mooo Moo

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

  4. USACO 2020 OPEN Silver Problem 3. The Moo Particle

    题意: 解法: 首先给出在本题中连通和连通块的定义: 连通: 两个粒子a,b连通,当且仅当ax≤bx.ay≤by或者bx≤ax.by≤ay. 如图,A,B两粒子是连通的,而C.D不是. 可以看出,本题 ...

  5. usaco silver

    大神们都在刷usaco,我也来水一水 1606: [Usaco2008 Dec]Hay For Sale 购买干草   裸背包 1607: [Usaco2008 Dec]Patting Heads 轻 ...

  6. Moo University - Financial Aid

    Moo University - Financial Aid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6020 Accep ...

  7. Bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 单调栈

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 631  Solved: 445[Submi ...

  8. BZOJ1657: [Usaco2006 Mar]Mooo 奶牛的歌声

    1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 489  Solved: 338[Submi ...

  9. poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)

    Description Bessie noted that although humans have many universities they can attend, cows have none ...

随机推荐

  1. ZROI1153 【线上训练3】数个数

    ZROI1153 [线上训练3]数个数 传送门 一道非常有意思的题,涵盖了各种知识点. 首先,很显然,这是个容斥.容斥可以过掉\(30pts\). 这里我们考虑容斥+DP. 我们令\(dp[i][j] ...

  2. HTML禁用Flash文件右键

    在项目中遇到一个需求,由于制作Flash的同事没有做禁用Flash文件右键功能!而Flash文件比较多,一个个改不太现实,于是要求用在网页显示的时候禁用Flash右键功能!未禁用之前! 禁用之前: 禁 ...

  3. 【C++】Debug模式和Release模式的区别

    VS中的程序有两种编译模式:Debug模式和Release模式. Debug通常称为调试版本,通过一系列编译选项的配合,编译结果通常包含调试信息,而且不做任何优化,以为开发人员提供强大的应用程序调试能 ...

  4. Appium+python自动化(五)- 模拟器(超详解)

    简介 Appium是做安卓自动化的一个比较流行的工具,对于想要学习该工具但是又局限于或许当前有些小伙伴没 android 手机来说,可以通过安卓模拟器来解决该问题,下面就讲解使用appium连接安卓模 ...

  5. pymysql模块常用操作

    pymysql安装 pip install pymysql 链接数据库.执行sql.关闭连接 import pymysql user = input('请输入用户名请输入密码:').strip() p ...

  6. 前端学习:学习笔记(JS部分)

    前端学习:学习笔记(JS部分) 前端学习:JS学习总结(图解)    JS的简介 JS基本语法 JS内置对象 JS的函数 JS的事件 JS的BOM JS的DOM JS的简介 新建步骤 <body ...

  7. 【06】Kubernets:资源清单(控制器 - Deployment)

    写在前面的话 上一节主要简单的提了一下控制器都有哪些常用的,并且简单的功能是啥,最后一并提了 ReplicaSet 控制器. 但是 ReplicaSet 一般不需要我们直接配置,多以从本节开始,开始学 ...

  8. .net Core 学习笔记(实体字段映射,IOC注入)

    https://github.com/wj1034184751/ADO.NetCore.git 先简单的做一个学习架子, EF层用的是 EFCore(2.1.0)+ Pomelo.EntityFram ...

  9. 输入www.baidu.com会发生什么

    1. 浏览器接收域名 2. 发送域名给DNS,请求解析出www.baidu.com的IP地址 中文名字是域名系统服务器,一般位于ISP(互联网服务提供商,比如我们熟知的联通.移动.电信等) 中.浏览器 ...

  10. C# 与 Java 的一些差异

    如果你是 Java 开发人员,则可以在 Xamarin 平台上充分利用你的技能和现有代码,同时获得 C# 的代码重用优势.你会发现 C# 语法与 Java 语法非常相似,这两种语言提供非常类似的功能. ...