Piggy-Bank

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23646    Accepted Submission(s): 11973

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.
 

Source

 
 //2017-04-04
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; const int inf = 0x3f3f3f3f;
int dp[], p[], w[];//dp[i]表示重量为i时最少的钱
//状态转移方程:dp[i] = min{dp[i-w[j]]+p[j] | 0<j<n} int main()
{
int T, n, E, F, W;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &E, &F);
W = F-E;
memset(dp, inf, sizeof(dp));
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d%d", &p[i], &w[i]);
if(p[i] < dp[w[i]])//考虑两种硬币重量相等时,选价值小的
dp[w[i]] = p[i];
}
for(int i = ; i <= W; i++){
for(int j = ; j < n; j++){
if(w[j] > i)continue;
if(dp[i-w[j]]+p[j] < dp[i])
dp[i] = dp[i-w[j]]+p[j];
}
}
if(dp[W] == inf)printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[W]);
} return ;
}

HDU1114(KB12-F DP)的更多相关文章

  1. hdu1114 Piggy-Bank (DP基础 完全背包)

    链接:Piggy-Bank 大意:已知一只猪存钱罐空的时候的重量.现在的重量,已知若干种钱的重量和价值,猪里面装着若干钱若干份,求猪中的钱的价值最小值. 题解: DP,完全背包. g[j]表示组成重量 ...

  2. Educational Codeforces Round 58 (Rated for Div. 2) F dp + 优化(新坑) + 离线处理

    https://codeforces.com/contest/1101/problem/F 题意 有n个城市,m辆卡车,每辆卡车有起点\(s_i\),终点\(f_i\),每公里油耗\(c_i\),可加 ...

  3. Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希

    https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...

  4. 【BZOJ4953】lydsy七月月赛 F DP

    [BZOJ4953]lydsy七月月赛 F 题面 题解:设f[i][j]表示第i个强度取为j时的最小误差.那么每次转移时,我们只计算j'和j之间的像素点带来的误差,于是有: $f[i][j]=min( ...

  5. hdu 4389 X mod f(x) 数位DP

    思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...

  6. HDU - 4389 X mod f(x)(数位dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...

  7. BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...

  8. F. Wi-Fi(线段树实现dp)

    题:http://codeforces.com/contest/1216/problem/F dp[i][0]:表示第i个位置不装的最小代价 dp[i][1]:表示第i个位置装的最小代价 T1的线段树 ...

  9. UVALive 6908---Electric Bike(DP或记录型深搜)

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  10. coderforces #384 D Chloe and pleasant prizes(DP)

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. MySQL随手记

    一.MySQL数据迁移(由远端主机迁移到本地) 1.导出数据库mysqldump -u root -p db > dump_db_date.sqlroot: 账户db: 需要导出的数据库名 2. ...

  2. 使用IEDA远程调试

    下面演示在docker中调试代码. 使用vulhub作为演示. 下载vulhub,进入vulhub/fastjson/vuln/ 编辑docker-compose.yml ,在最后新建一行加入  - ...

  3. [原创]K8_C段旁注查询工具5.0 30款国外主流CMS识别+智能识别未知CMS

    8_C段旁注查询工具V5.0 20161214作者:K8拉登哥哥 唯一QQ:396890445平台: Windows + .NET Framework 4.0 简介:K8_C段 提供4种方式查询子域名 ...

  4. POJ 2603

    #include<iostream> #include<stdio.h> #define M 350000 #define N 30000 using namespace st ...

  5. odoo开发笔记 -- 用户配置界面如何增加模块访问权限

    在odoo设置界面,点击用户,进入用户配置界面,会看到: 访问权 | 个人资料菜单 在访问权 page菜单界面,可以看到系统预制的一些模块都会显示在这里, 那么,我们自己开发的模块如何显示在这块呢,从 ...

  6. c++如何解决大数组栈内存不够的问题

    在c++中,我们可以直接通过下面的方式创建一个数组: ; ; ; double phi[N][Nx][Ny]; double phi_b[N][Nx][Ny]; 但是,如果上述的Nx和Ny比较小还好说 ...

  7. C# 多线程学习系列三之CLR线程池系列之ThreadPool

    一.CLR线程池 1.进程和CLR的关系一个进程可以只包含一个CLR,也可以包含多个CLR2.CLR和AppDomain的关系一个CLR可以包含多个AppDomain3.CLR和线程池的关系一个CLR ...

  8. Struts2+AJAX+JQuery 实现用户登入与注册功能。

    要求 必备知识 JAVA/Struts2,JS/JQuery,HTML/CSS基础语法. 开发环境 MyEclipse 10 演示地址 演示地址 预览截图(抬抬你的鼠标就可以看到演示地址哦): 关于U ...

  9. 基于boot2docker部署Docker环境

    Docker轻量级的特性使得我们可以快速打包开发环境:一处编译,到处使用.我们可以在第一次编译好需要的开发环境,然后把镜像导出打包,只有有docker环境,便可以快速还原原来的开发环境. 很常用的一个 ...

  10. linux文件命令汇总

    查看文件内容命令 cat 直接输出在命令行,适用于很少内容时候的输出, vim .vi  通过 编辑命令的只读模式进行文件内容的查看(翻页快捷键好像是 ctrl + F 下一页, ctrl + B 上 ...