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

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
USACO2022金组是国际在线判题系统USACO的最高级别,题目难度较高,在该比赛中取得好成绩是一项巨大的成就。以下是对该比赛的一些题目解析。 第一题:“交通计划” 题目要求:给定一个n个节点的有向图,每条边有一个长度,希望添加最少的边使得所有节点连通,求最小生成树的权值和。 解析:该题可以使用Kruskal算法求解,将每条边按权值从小到大排序,再依次加入,判断加入的边是否会形成环,若形成则不加入,直到所有节点连通为止。此时Kruskal算法得到的最小生成树的权值和即为所求。 第二题:“点火计划” 题目要求:给定一个n个节点的有向图,每条边有一个权值和一个点火时长,每个节点有一个点火启动时刻和时刻结束时刻,希望从其中选出一些边点火,使得所有节点都可从点火的边出发到达,且所选点火边的总点火时长最小。 解析:该题可以使用最小费用最大流算法求解。将每条边看做一个容量为1,费用为点火时长的边,源点向节点的点火边容量为1,费用为0的边,节点的点火边向汇点的容量为1,费用为0的边,对这个网络进行最小费用最大流即可得到所选边的总点火时长最小。 第三题:“美味佳肴” 题目要求:给定n个菜品,每个菜品有它的权值和两个类别,希望选出k个菜品,使得选出的菜品数量在每个类别中都不超过$\frac{k}{3}$个,且所选菜品的权值和最大。 解析:该题可以使用动态规划求解。设$f[i][j][k]$表示前i个菜品中,选择j个一类菜品,选择k个二类菜品的最大权值和,状态转移方程为$f[i][j][k]=max(f[i-1][j][k],f[i-1][j-1][k]+a[i],f[i-1][j][k-1]+b[i])$,其中a[i]为i号菜品的权值,若为一类则为该权值,否则为0,b[i]为i号菜品的权值,若为二类则为该权值,否则为0。最终答案为$f[n][$k/3$][$k/3$]。 以上是对USACO2022金组的部分题目的解析,USACO比赛是全球范围内的计算机竞赛,竞争非常激烈,能够在该比赛中脱颖而出是一项非常棒的成就。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值