hdu-1114 Piggy-Bank

文章目录

    • Problem Description
    • 题意:
    • 题解:
    • 代码:

hdu-1114

Problem Description

Before ACM can do anything, a budget must be prepared and the
necessary financial support obtained. The main income for this action
comes from Irreversibly Bound Money (IBM). The idea behind is simple.
Whenever some ACM member has any small money, he takes all the coins
and throws them into a piggy-bank. You know that this process is
irreversible, the coins cannot be removed without breaking the pig.
After a sufficiently long time, there should be enough cash in the
piggy-bank to pay everything that needs to be paid.

But there is a big problem with piggy-banks. It is not possible to
determine how much money is inside. So we might break the pig into
pieces only to find out that there is not enough money. Clearly, we
want to avoid this unpleasant situation. The only possibility is to
weigh the piggy-bank and try to guess how many coins are inside.
Assume that we are able to determine the weight of the pig exactly and
that we know the weights of all coins of a given currency. Then there
is some minimum amount of money in the piggy-bank that we can
guarantee. Your task is to find out this worst case and determine the
minimum amount of cash inside the piggy-bank. We need your help. No
more prematurely broken pigs!

Input

The input consists of T test cases. The number of them (T) is given on
the first line of the input file. Each test case begins with a line
containing two integers E and F. They indicate the weight of an empty
pig and of the pig filled with coins. Both weights are given in grams.
No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On
the second line of each test case, there is an integer number N (1 <=
N <= 500) that gives the number of various coins used in the given
currency. Following this are exactly N lines, each specifying one coin
type. These lines contain two integers each, Pand W (1 <= P <= 50000,
1 <= W <=10000). P is the value of the coin in monetary units, W is
it’s weight in grams.

Output

Print exactly one line of output for each test case. The line must
contain the sentence “The minimum amount of money in the piggy-bank is
X.” where X is the minimum amount of money that can be achieved using
coins with the given total weight. If the weight cannot be reached
exactly, print a line “This is impossible.”.

Sample Input

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

Sample Output

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

题意:

E和F分别表示空盒子和装满物品的盒子的质量,然后给你N件物品,每件物品的价值是P,质量是W,问装满这个盒子最少用总价值为多少的物品
分析样例:

10 110
2
1 1
30 50

要装的物品质量为110-10=100
装两个物品2(即价值30,质量50的这个物品),即可使得盒子装满,且总价值最低

题解:

物品有价值有质量,但是没有数量,没错就是完全背包问题
然后套上完全背包的板子
背包问题讲解
因为是求最小价值,记得将max改成min

代码:

懒得敲代码了,代码来自

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 500500;
int dp[INF],val[INF],we[INF];
int main(void)
{int t,n,i,j,m,st,ed,tp;cin>>t;while(t--){cin>>st>>ed;cin>>n;tp=ed-st;for(i=0;i<n;i++) cin>>val[i]>>we[i];memset(dp,INF,sizeof(dp));dp[0]=0;for(i=0;i<n;i++){for(j=we[i];j<=tp;j++){dp[j]=min(dp[j],dp[j-we[i]]+val[i]);}}if(dp[tp]!=dp[500100]) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[tp]);else printf("This is impossible.\n");}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/320021.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ATcoder-[AGC048B]Bracket Score【结论,贪心】

正题 题目链接:https://atcoder.jp/contests/agc048/tasks/agc048_b 题目大意 长度为nnn的合法括号序列可以包含[...][...][...]和(...)(...)(...)。 如果在第iii个位置是′(′\ (\ ′ ( ′ 或者 ′)′\ )\ ′ ) ′那么可以获得aia_iai​的权值&#xff0c;否则获得bib_ibi​…

.Net架构篇:实用中小型公司支付中心设计

前言说起支付平台&#xff0c;支付宝量级的支付平台和一个小型公司的支付不可同日耳语。一个初创或刚创业一两年的公司&#xff0c;一没人力&#xff0c;二没财力的情况下&#xff0c;如果也想对接支付那怎么办呢&#xff1f;感谢支付宝和微信支付&#xff0c;两大行业巨头提供…

【期望】选书问题(金牌导航 期望-7)

选书问题 金牌导航 期望-7 题目大意 有n个人&#xff0c;每个人有自己的选书目录&#xff0c;一个人有p的概率选当前的书&#xff0c;有1-p的概率不选&#xff0c;即去查看下一本书&#xff08;过n后回到1&#xff09;&#xff0c;现在问你选书的逆序对的期望数 输入样例 …

[CSA49G][XSY3315] Bunny on Number Line (DP)

CSA49G XSY3315 因为判断两串是否本质不同只看某几项是不是好数&#xff0c;与究竟是哪个好数无关&#xff0c;所以考虑转换一下题意&#xff1a; 给出一个长度为aka_kak​的01串SSS&#xff0c;第a1,a2,...,aka_1,a_2,...,a_ka1​,a2​,...,ak​项为1&#xff0c;其余项为0。 …

Rabbit的工作(2)

牛客网 文章目录题目描述题解&#xff1a;代码&#xff1a;题目描述 Rabbit通过了上次boss的考核&#xff0c;现在她又遇到了一个问题。 Rabbit接到了K个任务&#xff0c;每个任务她可以自由选择用i天去完成(1≤ i≤ N)。刁钻的boss想让Rabbit恰好用W天完成所有任务。 已知Rabb…

jzoj6065-[NOI2019模拟2019.3.18]One?One!【FFT】

正题 题目链接:https://gmoj.net/senior/#main/show/6065 题目大意 oneness(x)oneness(x)oneness(x)表示xxx的约数中全是111的数的个数&#xff0c;给出一个长度为lll的随机生成的数nnn&#xff0c;求∑i1noneness(i)\sum_{i1}^noneness(i)i1∑n​oneness(i) 解题思路 转换一…

netcore编程之后面对不习惯的xshell黑屏部署,是时候使用jenkins自动化发布工具了...

在很久之前net还只能在windows上部署的时候&#xff0c;或许很多创业公司的朋友发布项目还都是人肉部署&#xff0c;反正windows都是可视化的界面&#xff0c;拖拖拉拉&#xff0c;开开关关还不是特别麻烦。。。现如今你的项目需要在linux上部署&#xff0c;可惜的是再也没有什…

【期望】关灯游戏(金牌导航 期望-8)

关灯游戏 金牌导航 期望-8 题目大意 有n盏灯&#xff0c;有些是亮的&#xff0c;有的是暗的&#xff0c;现在如果按一个位置的开关&#xff0c;那么是它因数的位置的灯都会改变开关情况&#xff0c;现在如果用k步不能直接关完&#xff0c;就随机按&#xff0c;直到可以k步关…

【招聘(重庆)】新空间(重庆)科技有限公司招聘.NET Core

全新平台公司&#xff0c;技术氛围好&#xff0c;未来上升空间巨大!平台架构师薪资范围&#xff1a;15K至40K岗位职责&#xff1a;1、负责公司业务以及相关平台的架构设计、技术选型、研发工作, 参与产品架构的规划与设计&#xff1b;2、遵循总体的架构规划与规范设计项目的应用…

jzoj6067-[NOI2019模拟2019.3.18]More?More!【dp】

正题 题目链接:https://gmoj.net/senior/#main/show/6067 题目大意 nnn个点的一张竞赛图&#xff0c;第iii个点向第jjj个点(i<j)(i<j)(i<j)连边的概率是ppp&#xff0c;否则就是第jjj个点向第iii个点连边。 对于每个i(i<n)i(i<n)i(i<n)求出能够选出一个大小…

[XSY3320] string (AC自动机,哈希,点分治)

XSY3320 前置芝士&#xff1a;回文前缀&&borderborderborder 推荐博客 推荐博客 考虑点分治&#xff0c;问题变成求经过重心的回文路径个数。 一条经过重心的回文路径长这样&#xff1a; xxx到zzz的串与yyy到rootrootroot的串相同。 建出根到每个节点对应的串的AC自…

【期望】守卫挑战(金牌导航 期望-9)

守卫挑战 金牌导航 期望-9 题目大意 有n个数&#xff0c;到第i个数&#xff0c;有p_i的概率选择这个数&#xff0c;问你最后选了最少L个数&#xff0c;且选的数的和再加k大于等于0 样例输入 3 1 0 10 20 30 -1 -1 2样例输出 0.300000数据范围 0⩽k⩽20000\leqslant k\leq…

xinjun与阴阳师

来源&#xff1a;牛客网 文章目录题目描述题解&#xff1a;代码&#xff1a;题目描述 xinjun是各类手游的狂热粉丝&#xff0c;因随手一氪、一氪上千而威震工大&#xff0c;现在他迷上了阴阳师。xinjun玩手游有一个习惯&#xff0c;就是经过层层计算制定出一套方案来使操作利益…

P3311-[SDOI2014]数数【AC自动机,dp】

正题 题目链接:https://www.luogu.com.cn/problem/P3311 题目大意 求一个不超过nnn的数字使其不包含任何sss集合中的数字串。 解题思路 很经典的ACACAC自动机上面dpdpdp&#xff0c;但是因为是数字所以要来点数位dpdpdp的东西&#xff0c;多开一维用0/10/10/1表示是否在上界…

如何在 ASP.Net Core 中使用 Consul 来存储配置

原文: USING CONSUL FOR STORING THE CONFIGURATION IN ASP.NET CORE作者: Nathanael[译者注&#xff1a;因急于分享给大家&#xff0c;所以本文翻译的很仓促&#xff0c;有些不准确的地方还望谅解]来自 Hashicorp 公司的 Consul 是一个用于分布式架构的工具&#xff0c;可以用…

[XSY3381] 踢罐子(几何)

XSY3381 点被选为点对之一的贡献我们单独计算&#xff08;这部分贡献的总和为4n(n−1)(n−2)4n(n-1)(n-2)4n(n−1)(n−2)&#xff09;。接下来只讨论剩余部分的贡献。 先把任意三个点构成的六种选择方案合并&#xff0c;发现在外接圆周和弦之间的点每个有2的贡献&#xff0c;…

GDKOI2021总结

前言 因为疫情的原因&#xff0c;以学校为单位在校参加&#xff01;&#xff08;就很秃然&#xff09; 注&#xff1a;前三天是普及组&#xff0c;后三天是提高组 Day 1 第一次参加&#xff0c;以为会被虐得成狗&#xff0c;以忐忑的心情打开T1 4⩽n⩽20004\leqslant n\leqs…

The Bottom of a Graph Poj 2553

牛客网 poj 2553 文章目录Description题意&#xff1a;题解&#xff1a;代码&#xff1a;Description We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be …

P7244-章节划分【RMQ,贪心,递归】

正题 题目链接:https://www.luogu.com.cn/problem/P7244?contestId38911 题目大意 nnn个数字&#xff0c;分成连续非空的kkk段要求每一段的最大值的gcdgcdgcd最大。 解题思路 首先答案一定是最大值的约数&#xff0c;这些数不多我们可以枚举这些数xxx。然后我们称xxx的倍数的…

[XSY3382] 专家系统(二分+线段树)

XSY3382 二分ccc&#xff0c;问题变成能否用一个长ccc宽ccc的矩形框住至少kkk个点。 二维数点问题考虑用扫描线解决。将所有点按照xxx从小到大排序。 枚举一段xxx坐标相差不超过ccc的点&#xff08;双指针推进&#xff09;&#xff0c;初始想法是根据这些点的yyy值建一棵权值…