HDU1114(KB12-F DP)
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
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
Output
Sample Input
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 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)的更多相关文章
- hdu1114 Piggy-Bank (DP基础 完全背包)
链接:Piggy-Bank 大意:已知一只猪存钱罐空的时候的重量.现在的重量,已知若干种钱的重量和价值,猪里面装着若干钱若干份,求猪中的钱的价值最小值. 题解: DP,完全背包. g[j]表示组成重量 ...
- 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\),可加 ...
- Codeforces Round #543 (Div. 2) F dp + 二分 + 字符串哈希
https://codeforces.com/contest/1121/problem/F 题意 给你一个有n(<=5000)个字符的串,有两种压缩字符的方法: 1. 压缩单一字符,代价为a 2 ...
- 【BZOJ4953】lydsy七月月赛 F DP
[BZOJ4953]lydsy七月月赛 F 题面 题解:设f[i][j]表示第i个强度取为j时的最小误差.那么每次转移时,我们只计算j'和j之间的像素点带来的误差,于是有: $f[i][j]=min( ...
- hdu 4389 X mod f(x) 数位DP
思路: 每次枚举数字和也就是取模的f(x),这样方便计算. 其他就是基本的数位Dp了. 代码如下: #include<iostream> #include<stdio.h> # ...
- HDU - 4389 X mod f(x)(数位dp)
http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...
- BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...
- F. Wi-Fi(线段树实现dp)
题:http://codeforces.com/contest/1216/problem/F dp[i][0]:表示第i个位置不装的最小代价 dp[i][1]:表示第i个位置装的最小代价 T1的线段树 ...
- UVALive 6908---Electric Bike(DP或记录型深搜)
题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- 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 ...
随机推荐
- 【UML】:时序图
时序图表达了类之间调用关系,以及调用时序关系. Actor: 调用者实例化类的对象,执行者. Lifeline: 生命线,竖的虚线.上方方框是类名表示存在的时间,从上至下表示时间流逝.Lifeline ...
- 如何进行bug总结
在项目过程中,测试同学会发现大量的bug,但同时也不可避免的会存在一些遗漏的bug.为了能够减少遗漏bug的现象,我们需要针对遗漏的问题进行总结,从教训中积累经验,总结方法,从而提高测试的覆盖度,提升 ...
- 条件变量 sync.Cond
sync.Cond 条件变量是基于互斥锁的,它必须有互斥锁的支撑才能发挥作用. sync.Cond 条件变量用来协调想要访问共享资源的那些线程,当共享资源的状态发生变化的时候,它可以用来通知被互斥锁阻 ...
- 设置多台机器linux服务器ssh相互无密码访问
在每台服务器上都执行ssh-keygen -t rsa生成密钥对: $ ssh-keygen -t rsa Generating public/private rsa key pair. Enter ...
- iOS UIFileSharingEnabled
一.让iOS App通过iTunes进行文件交换Documents 让iOS App通过iTunes进行文件交换 有一些App需要通过使用iTunes让用户上传和下载文档.要让iOS程序支持iTune ...
- 【sping揭秘】13、Spring AOP一世&二世
Spring AOP一世 Spring AOP中的advice Before advice 这个就是在方法执行之前执行,也就是在对应的joinpoint之前 spring AOP二世 开启aspect ...
- Python获取二维数组的行列数
import numpy as np x = np.array([[1,2,5],[2,3,5],[3,4,5],[2,3,6]]) # 输出数组的行和列数 print x.shape # (4, 3 ...
- SQL Server性能优化(6)查询语句建议
1. 如果对数据不是工业级的访问(允许脏读),在select里添加 with(nolock) ID FROM Measure_heat WITH (nolock) 2. 限制结果集的数据量,如使用TO ...
- RETE算法介绍
RETE算法介绍一. rete概述Rete算法是一种前向规则快速匹配算法,其匹配速度与规则数目无关.Rete是拉丁文,对应英文是net,也就是网络.Rete算法通过形成一个rete网络进行模式匹配,利 ...
- java Queue的用法
https://www.cnblogs.com/caozengling/p/5307992.html https://blog.csdn.net/a724888/article/details/802 ...