【USACO 5.3.1】量取牛奶
农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出。消费者要多少,他就给多少,从不有任何误差。
农夫约翰总是很节约。他现在在奶牛五金商店购买一些桶,用来从他的巨大的牛奶池中量出 Q 夸脱的牛奶。每个桶的价格一样。你的任务是计算出一个农夫约翰可以购买的最少的桶的集合,使得能够刚好用这些桶量出 Q 夸脱的牛奶。另外,由于农夫约翰必须把这些桶搬回家,对于给出的两个极小桶集合,他会选择“更小的”一个,即:把这两个集合按升序排序,比较第一个桶,选择第一个桶容积较小的一个。如果第一个桶相同,比较第二个桶,也按上面的方法选择。否则继续这样的工作,直到相比较的两个桶不一致为止。例如,集合 {3,5,7,100} 比集合 {3,6,7,8} 要好。
为了量出牛奶,农夫约翰可以从牛奶池把桶装满,然后倒进瓶子。他决不把瓶子里的牛奶倒出来或者把桶里的牛奶倒到别处。用一个容积为 1 夸脱的桶,农夫约翰可以只用这个桶量出所有可能的夸脱数。其它的桶的组合没有这么方便。 计算需要购买的最佳桶集,保证所有的测试数据都至少有一个解。
Input
Line 1: 一个整数 Q
Line 2: 一个整数P(1 <= P <= 100),表示商店里桶的数量
Lines 3..P+2: 每行包括一个桶的容积(1 <= 桶的容积 <= 10000)
Output
输出只有一行,由空格分开的整数组成:
为了量出想要的夸脱数,需要购买的最少的桶的数量,接着是:一个排好序的列表(从小到大),表示需要购买的每个桶的容积
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<cstring>
using namespace std;
int dp[];
int tong[];
int use[];
bool b[];
int q,p,flag=;
bool judge(int num){
dp[]=;
for(int i=;i<=q;i++) dp[i]=;
for(int i=;i<=num;i++){
for(int j=use[i];j<=q;j++){
dp[j]=dp[j]|dp[j-use[i]];
}
}
if(dp[q]==) return ;
return ;
}
void print(int num){
printf("%d ",num);
sort(use+,use+num);
for(int i=;i<=num-;i++){
printf("%d ",use[i]);
}
}
void dfs(int now,int top){
if(now==top){
if(judge(top-)){
print(top);
exit();
}
return;
}
for(int i=;i<=p;i++){
if(b[i]) continue;
b[i]=;
use[now]=tong[i];
dfs(now+,top);
b[i]=;
use[now]=;
}
}
int main(){
memset(dp,,sizeof(dp));
memset(use,,sizeof(use));
memset(b,,sizeof(b));
scanf("%d%d",&q,&p);
for(int i=;i<=p;i++){
cin>>tong[i];
}
sort(tong+,tong+p+);
for(int top=;top<=p;top++){
memset(b,,sizeof(b));
dfs(,top);
}
return ;
}
- #include<iostream>
- #include<stdio.h>
- #include<stdlib.h>
- #include<algorithm>
- #include<cstring>
- usingnamespace std;
- int dp[20010];
- int tong[110];
- int use[110];
- bool b[110];
- int q,p,flag=0;
- bool judge(int num){
- dp[0]=1;
- for(int i=1;i<=q;i++) dp[i]=0;
- for(int i=0;i<=num;i++){
- for(int j=use[i];j<=q;j++){
- dp[j]=dp[j]|dp[j-use[i]];
- }
- }
- if(dp[q]==1)return1;
- return0;
- }
- void print(int num){
- printf("%d ",num);
- sort(use+0,use+num);
- for(int i=0;i<=num-1;i++){
- printf("%d ",use[i]);
- }
- }
- void dfs(int now,int top){
- if(now==top){
- if(judge(top-1)){
- print(top);
- exit(0);
- }
- return;
- }
- for(int i=1;i<=p;i++){
- if(b[i])continue;
- b[i]=1;
- use[now]=tong[i];
- dfs(now+1,top);
- b[i]=0;
- use[now]=0;
- }
- }
- int main(){
- memset(dp,0,sizeof(dp));
- memset(use,0,sizeof(use));
- memset(b,0,sizeof(b));
- scanf("%d%d",&q,&p);
- for(int i=1;i<=p;i++){
- cin>>tong[i];
- }
- sort(tong+1,tong+p+1);
- for(int top=1;top<=p;top++){
- memset(b,0,sizeof(b));
- dfs(0,top);
- }
- return0;
- }
【USACO 5.3.1】量取牛奶的更多相关文章
- [USACO Section 5.3]量取牛奶 Milk Measuring (动态规划,背包$dp$)
题目链接 Solution 完全背包 \(dp\) , 同时再加一个数组 \(v[i][j]\) 记录当总和为\(j\) 时第 \(i\) 种物品是否被选. 为保证从小到大和字典序,先将瓶子按大小排序 ...
- 洛谷 P2744 [USACO5.3]量取牛奶Milk Measuring
P2744 [USACO5.3]量取牛奶Milk Measuring 题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最 ...
- 【USACO 5.3.1】量取牛奶 迭代
Description 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就 ...
- [USACO5.3]量取牛奶Milk Measuring
https://daniu.luogu.org/problemnew/show/P2744 滚动数组压去第一维:前i种木桶 f[j] 量取体积j最少需要几种木桶 g[j] 体积j的最优解是否使用了第 ...
- luogu P2744 [USACO5.3]量取牛奶Milk Measuring
题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位——译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...
- 【洛谷2744 】【CJOJ1804】[USACO5.3]量取牛奶Milk Measuring
题面 Description 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位--译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少 ...
- 洛谷P2744 [USACO5.3]量取牛奶Milk Measuring
题目描述 农夫约翰要量取 Q(1 <= Q <= 20,000)夸脱(夸脱,quarts,容积单位--译者注) 他的最好的牛奶,并把它装入一个大瓶子中卖出.消费者要多少,他就给多少,从不有 ...
- [luoguP1494] 岳麓山上打水 && [luoguP2744] [USACO5.3]量取牛奶Milk Measuring
传送门 传送门 dfs选取集合,dp背包判断 虽然我觉的会TLE.. 但是的确是AC了 #include <cstdio> #include <cstring> #includ ...
- luogu2744 量取牛奶
题目大意 给出一个整数集合$A$,总数$N$,规定一个整数序列$\{a_n\}, \forall i, a_i\in A$满足条件:存在一个正整数序列$\{k_n\}$,使得$\sum_{i=1}^n ...
随机推荐
- 【LeetCode】56-合并区间
题目描述 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 ...
- Redis字符串键的底层原理
before C语言基础 Redis基础 导入 redis的命令如下: set x "hello"; get x; hello Redis作为一种存储字符串的缓存结构,其具体实现是 ...
- .Net基础篇_学习笔记_第六天_for循环的嵌套_乘法口诀表
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Winform中自定义xml配置文件后对节点进行读取与写入
场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 ...
- 4、链栈的实现(java代码)
1.链节点 public class Node<T> { public T data; public Node next; } 2.实现代码 public class Stack<T ...
- 怎么在本地建立一个Maven 项目push到码云(https://git.oschina.net)
本地建立一个的mvan项目不使用SmartGit push到码云上. 1 首先在自己码云的建立一个maven 空项目 2 然后打开STS(Spring Tool Suite) 新建一个Maven( ...
- 当 K8s 集群达到万级规模,阿里巴巴如何解决系统各组件性能问题?
作者 | 阿里云容器平台高级技术专家 曾凡松(逐灵) 本文主要介绍阿里巴巴在大规模生产环境中落地 Kubernetes 的过程中,在集群规模上遇到的典型问题以及对应的解决方案,内容包含对 etcd.k ...
- [C++] 重载运算符与类型转换(2)——函数调用运算符和类型转换运算符
1.这两个应该是C++中比较高级的用法了. 一.函数调用运算符 1.重载函数调用运算符(),必须是成员函数,一个类可以定义多个不同版本的调用运算符,相互之间应该在参数数量或者类型上有所区别. ...
- HTML连载39-外边距合并现象、盒子模型以及宽度和高度
一. 在默认布局的垂直方向上,默认情况下外边距是是不会叠加的,会出现合并现象,谁的外边距较大,就听谁的:但是在水平方向就不会出现这种状况,我们举个例子 span{ display: inline-bl ...
- 程序员接触新语言————hello world ^-^,web3种样式表
我的第一个网页 <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...