首页 > 其他分享 >hdu:Piggy-Bank(背包)

hdu:Piggy-Bank(背包)

时间:2023-08-26 17:22:33浏览次数:37  
标签:hdu Bank int money coins amount Piggy bank piggy

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.

背包

完全背包加背包装满判断

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
const int suf=0x3f3f3f3f;
int c[N],w[N],dp[N];
void solve()
{
    int E,F;
    cin>>E>>F;
    int v=F-E;
    int n;
    cin>>n;
    memset(dp,0x3f,sizeof dp);
    for(int i=0;i<n;++i) cin>>w[i]>>c[i];
    dp[0]=0;
    for(int i=0;i<n;++i)
     for(int j=c[i];j<=v;++j)
     {
     	if(dp[j-c[i]]!=suf) dp[j]=min(dp[j],dp[j-c[i]]+w[i]);
     }
    if(dp[v]==suf) cout<<"This is impossible.\n";
    else cout<<"The minimum amount of money in the piggy-bank is "<<dp[v]<<".\n";
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }
    return 0;
}

标签:hdu,Bank,int,money,coins,amount,Piggy,bank,piggy
From: https://www.cnblogs.com/ruoye123456/p/17659134.html

相关文章

  • hdu:免费馅饼
    ProblemDescription都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼。说来gameboy的人品实在是太好了,这馅饼别处都不掉,就掉落在他身旁的10米范围内。馅饼如果掉在了地上当然就不能吃了,所以gameboy马上卸下身上的背包去接。但由于小径两侧都不能......
  • hdu:搬寝室
    ProblemDescription搬寝室是很累的,xhd深有体会.时间追述2006年7月9号,那天xhd迫于无奈要从27号楼搬到3号楼,因为10号要封楼了.看着寝室里的n件物品,xhd开始发呆,因为n是一个小于2000的整数,实在是太多了,于是xhd决定随便搬2k件过去就行了.但还是会很累,因为2k也不小是一个不大......
  • hdu:不容易系列之(3)—— LELE的RPG难题
    ProblemDescription人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即”可乐”),经过多方打探,某资深Cole终于知道了原因,原来,LELE最近研究起了著名的RPG难题:有排成一行的n个方格,用红(Red)、粉(Pink)、绿(Green)三色涂每个格子,每格涂一色,要......
  • hdu:畅通工程(并查集)
    ProblemDescription某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇。省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间接通过道路可达即可)。问最少还需要建设多少条道路?Input测试输入包含若干......
  • hdu:田忌赛马(贪心,双指针)
    ProblemDescription“田忌赛马”是中国历史上一个著名的故事。大约2300年前,齐国大将田忌喜欢和国王赛马,并且约定:每赢一场,对方就要付200元。假设已知田忌和国王的各自马匹的速度都不相同,请计算田忌最好的结果是什么。Input输入包含多组测试样例。每组样例的第一行是一个整数......
  • hdu:老鼠和猫的交易(贪心)
    ProblemDescription小老鼠准备了M磅的猫粮,准备去和看守仓库的猫做交易,因为仓库里有小老鼠喜欢吃的五香豆。仓库有N个房间;第i个房间有J[i]磅的五香豆,并且需要用F[i]磅的猫粮去交换;老鼠不必交换该房间所有的五香豆,换句话说,它可以用F[i]a%磅的猫粮去换取J[i]a%磅的五香豆,其......
  • hdu 1003 最大最长上升子序列 贪心
    要想找到符合条件的序列,我们应该有以下条件 一个数重头开始遍历相加,如果这个数大于0的话,继续加后面的数,如果小于0的话,重后面的数开始重新遍历;这个过程中保证了大数一定会出现,所以应该找出大数;sum大于0的话,与后面的数相加有可能是最大数;如果小于0,则,重新开始会比以前的数更大;一下是......
  • hdu 1003 最大最长子序列 dp
    我的dp思路是记b[j]表示到到j位,最大最长的子序列的和则可得状态转移方程b[j]=max(b[j-1]+a[j],a[j]);因为每个数都有两种状态,要么和前面相连,要么自己相连;让后再比较出来最大值;一下是我的代码#include<stdio.h>#include<stdlib.h>#include<stdlib.h>#include<math.h>#includ......
  • hdu 4055
    http://acm.hdu.edu.cn/showproblem.php?pid=4055#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>usingnamespacestd;constintmod=1000000007;constintsiz......
  • hdu 4055 dp
    http://acm.hdu.edu.cn/showproblem.php?pid=4055#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<iostream>#include<algorithm>usingnamespacestd;constintmod=1000000007;cons......