Piggy-Bank【暑期集训F题】【完全背包】

2023-11-12

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! 
InputThe 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. 
OutputPrint 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.

        这道题要做的就是两件事:

(一)、求出所给的存钱罐中的质量(满存钱罐的值-空存钱罐的值)能否被所给硬币填满,若能,则证明可以被输出值,不然就是impossible!

(二)、在(一)存在条件下,我们去求满存钱罐时的最小值,也就是最坏情况,用一个完全背包即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int idx=1e9+5;
int coin_w;   //硬币的实际总质量
int N;
int p[505],w[505];   //p是货币值,w是质量
int dp[10005];
int wp[10005];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        for(int i=0; i<10004; i++)
        {
            dp[i]=idx;
        }
        dp[0]=0;
        memset(wp, 0, sizeof(wp));
        int r1,r2;
        scanf("%d%d",&r1,&r2);
        coin_w=r2-r1;
        scanf("%d",&N);
        for(int i=0; i<N; i++)
        {
            scanf("%d%d",&p[i],&w[i]);
        }
        for(int i=0; i<N; i++)
        {
            for(int j=w[i]; j<=coin_w; j++)
            {
                if(dp[j]>dp[j-w[i]]+p[i])
                {
                    dp[j]=dp[j-w[i]]+p[i];
                }
            }
        }
        for(int i=0; i<N; i++)
        {
            for(int j=w[i]; j<=coin_w; j++)
            {
                if(wp[j]<wp[j-w[i]]+w[i])
                {
                    wp[j]=wp[j-w[i]]+w[i];
                }
            }
        }
        if(wp[coin_w]==coin_w)
        {
            printf("The minimum amount of money in the piggy-bank is %d.\n",dp[coin_w]);
        }
        else
        {
            printf("This is impossible.\n");
        }
    }
    return 0;
}


           最近有了些特殊的想法,对于代码做了些小改动,这样就不用再花一个数组去记录重量了:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int real_weight;
int N;
struct node
{
    int v,w;
}a[505];
int dp[10005];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(a, 0, sizeof(a));
        memset(dp, 0, sizeof(dp));
        int e1,e2;
        scanf("%d%d",&e1,&e2);
        real_weight=e2-e1;
        scanf("%d",&N);
        for(int i=0; i<N; i++)
        {
            scanf("%d%d",&a[i].v,&a[i].w);
        }
        for(int i=0; i<N; i++)
        {
            for(int j=a[i].w; j<=real_weight; j++)
            {
                if(dp[j-a[i].w] || !(j-a[i].w))
                {
                    if(!dp[j])dp[j]=dp[j-a[i].w]+a[i].v;
                    dp[j]=min(dp[j], dp[j-a[i].w]+a[i].v);
                }
            }
        }
        if(dp[real_weight]) printf("The minimum amount of money in the piggy-bank is %d.\n",dp[real_weight]);
        else printf("This is impossible.\n");
    }
    return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Piggy-Bank【暑期集训F题】【完全背包】 的相关文章

  • 数学期望(离散型和连续型)

    数学期望的定义 数学期望的计算公式 例题 1 数学期望的定义 在概率论和统计学中 数学期望 或均值 是试验中每次可能结果的概率乘以其结果的总和 是最基本的数学特征之一 它反映随机变量平均取值的大小 随机变量包括离散型和连续型 数学期望的计算
  • 如何玩转kvm切换器

    KVM多电脑切换器适用对象涵盖SOHO 小型工作室 族群 中小型企业乃至于大型跨国企业 KVM多电脑切换器对于企业机房或数据中心的空间及信息环境能创造广大的效益 不仅能降低能源消耗 节省机架与机房空间 还能避免多余的键盘 显示器与鼠标所造成
  • linux内核有哪些协议族,Linux内核中PF_KEY协议族的实现.doc

    Linux内核中PF KEY协议族的实现 Linux内核中PF KEY协议族的实现 1 本文档的Copyleft归yfydz所有 使用GPL发布 可以自由拷贝 转载 转载时请保持文档的完整性 严禁用于任何商业用途 msn yfydz no1
  • libvlc —— 攫取 RGB图像 和 PCM音频 数据[C++代码实现]

    在我以前的实际项目中 曾利用 libvlc 去解码音视频媒体数据 如 RTSP 本地文件 等 通过其提供的回调函数接口 攫取 RGB图像 进行图像分析 如 人脸识别 运动检测 等一类的产品应用 除此之外 只要提供适当的 MRL 配合选项参数
  • sm2算法前端处理_超级账本 Fabric 国密算法支持

    区块链高级技术专家群内部讲座系列活动 群内由区块链相关团队或组织的技术专家 学者和负责人等组成 目前仅限邀请加入 分享内容会在 TechFirst 微信公众号进行首发 欢迎关注 嘉宾介绍 刘地军 现就职于中国网安密码国家重点实验室 负责和参
  • poj 1195 Mobile phones

    Problem poj org problem id 1195 vjudge net contest 146952 problem C Meaning 有一个 S S 的正方形区域 两维的下标范围都是是 0 S 1 有 4 种操作 1 0
  • git图形化工具GitKraken的使用——Stash和Pop

    正如两个单词的字面意思一样 stash 贮藏 pop 将准备好的东西突然拿出来 这一节模拟git中的这两个命令 git stash 和 git stash pop 在实际开发中 解决bug是避免不了的 在git中 每个bug都是通过新建一个
  • 2022年9月电子学会C语言等级考试试卷(二级)答案解析

    青少年软件编程 C语言 等级考试试卷 二级 分数 100 题数 5 1 统计误差范围内的数 统计一个整数序列中与指定数字m误差范围小于等于X的数的个数 时间限制 5000 内存限制 65536 输入 输入包含三行 第一行为N 表示整数序列的

随机推荐

  • 分治法和蛮力法MATLAB求最近点对

    主程序 main m clear clc n 20 随机生成20个点 A rand n 2 10 将20个点按横坐标升序排列 A sortrows A 1 蛮力法求随机点的最近点对 mindist x1 x2 Bcloest A 1 n m
  • constraintlayout嵌套_Android开发知识(二十六)强大的约束布局 - ConstraintLayout的用法总结...

    th 0dp android layout height 0dp app layout constraintHeight percent 0 5 app layout constraintHei oid layout height 0dp
  • 中继的框架与介绍

    一 概述 继 Relay 是一种网络设备或服务 用于转发网络数据包或消息 它在计算机网络中起到桥接 转发或中转的作用 将信息从一个地方传递到另一个地方 中继可以用于不同类型的网络 包括局域网 LAN 广域网 WAN 互联网等 它可以在不同网
  • Pycharm配置本地解释器

    由于Pycharm自带解释器 所以默认情况下我们是无法使用本地安装好的第三方库的 这个时候我们需要在Pycharm中配置本地的解释器 1 setting 2 add 3 找到本地的python解释器的路径
  • 超级详细找CALL写CALL教程[转]

    首先我们要知道一点 为什么要找CALL CALL是什么 大家知道易里的子程序吧如何调用子程序的 这里的CALL就是调用子程序的意思 那问了为什么要找他的 答案是 当你些个游戏的外挂用模拟键盘操作的时候 被操作的永远是当前窗口 当窗口切换的时
  • 多元时间序列因果关系分析研究综述

    Granger因果分析基本方法 目录 Granger因果分析基本方法 条件 Granger 因果模型 多元混沌时间序列因果分析 高维时间序列的因果分析 Lasso Granger因果模型 非线性Granger因果模型 Granger因果关系
  • DHCP 理论

    DHCP的基本工作过程 有4个阶段 discover offer request ack nak 抓包 标准地址池 1 地址段 网络号 掩码 2 网关 用于不同网段通信 3 dns DHCP的offer包部分字段 option 1 掩码 o
  • 讯飞星火认知大模型可以内测了

    以ChatGPT为代表的AI产品层出不穷 每天在社交媒体都可以看到AI领域的新成果 写文章 写代码 绘画 各种功能让人大呼神奇 4月24日 讯飞星火认知大模型来了 只需一个指令 懂你所言 答你所问 创你所需 解你所难 学你所教 一旦掌握正确
  • scp传输文件的命令

    scp传输文件的命令 scp传输文件的命令 一 scp常规的使用方式 scp可以进行简单的远程复制文件的功能 它是一个在各个主机之间进行复制或文件传输的一个命令工具 它使用一种同ssh一样的安全机制来进行文件的传输 注意 下面定义的远程计算
  • 云计算day08-Kubernetes_K8s

    文章目录 1 k8s的架构 2 k8s集群的安装 2 1 环境准备 2 2 k8s master上配置 2 3 master节点安装kubernetes 2 4 node节点安装kubernetes 2 5 所有节点配置flannel网络
  • 简单阐述下决策树、回归、SVM、神经网络等算法各自的优缺点?

    正则化算法 Regularization Algorithms 集成算法 Ensemble Algorithms 决策树算法 Decision Tree Algorithm 回归 Regression 人工神经网络 Artificial N
  • 检测浏览器是否开启firebug以及如何避免调试信息带来的脚本错误

    今天发现使用Gmail的时候开启firebug 会给出提示 在已知情况下 除非正确配置 Firebug 否则它会使 Gmail 运行缓慢 解决此问题 隐藏 感叹Gmail真是事无巨细 面面都考虑到了 于是想了解Gmail是如何检测用户是否开
  • vue解决Not allowed to load local resource

    前言 在进行通过本地路径进行加载图片的时候 突然就报了这个问题 Not allowed to load local resource 这个是由于安全性的问题 导致浏览器禁止直接访问本地文件 那么 这边我说一下我具体是怎么解决的吧 问题描述
  • linux alien命令将deb安装包和rpm安装包进行相互转换

    alien命令作用 alien是一个用于在各种不同的Linux包格式相互转换的工具 其最常见的用法是将 rpm转换成 deb 或者反过来 alien命令安装 Debian系linux可使用下面命令安装alien sudo apt get i
  • 分享一下我做软件测试这些年的心路历程,以及软件测试的发展方向。

    为什么入软件这行 很多人问我 一个女孩子做这个不太好 做不长久 特别年龄大了更不好做 我只是很随意的说专业对口 我能说是看上这个行业的高工资和技术范么 这样太俗了 然而就是这个俗气的理由让我走上这一条路 且想一直走下去 为什么呢 一是因为做
  • Circuit Board

    http acm zju edu cn onlinejudge showProblem do problemCode 164 On the circuit board there are lots of circuit paths We k
  • NDK使用遇到的那些事(持续更新当中)

    AppCamera transformNativeLibsWithStripDebugSymbolForDebug FAILURE Build failed with an exception What went wrong Executi
  • 性能、自动化面试题

    1 性能测试流程是怎么样的 2 如果测试过程中发现响应时间比较长 怎么分析 1 排查负载机 是不是负载机资源不足引起的 看看脚本是不是有问题 2 查看所消耗的时间主要是在网络传输上还是服务器上 网络传输 结合网络吞吐量图计算宽带是不是有瓶颈
  • 非spring注入使用RedisTemplate,需先调用afterPropertiesSet()方法

    错误信息 Exception in thread main java lang IllegalArgumentException template not initialized call afterPropertiesSet before
  • Piggy-Bank【暑期集训F题】【完全背包】

    Before ACM can do anything a budget must be prepared and the necessary financial support obtained The main income for th